Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following C code?

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10;
        foo((&i)++);
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

 

  • A.10
  • B.Some garbage value
  • C.Compile time error
  • D.Segmentation fault/code crash

Answer: C

Correct answer: Compile time error

&i yields the address of i, but it is a value, not an object - there is no storage holding it that could be incremented.

++ requires a modifiable lvalue, so (&i)++ is a constraint violation and the compiler rejects it. The contrast with the next case is instructive: if the address is first stored in a pointer variable, that variable is an object and can be incremented.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.