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

Discussion

What is the output of this C code?

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 is a value, not an object, so it cannot be incremented: the ++ operator needs a modifiable lvalue and the result of & is neither modifiable nor an lvalue. The next question shows the working version, in which a pointer variable holds the address and it is the variable that is incremented.

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

Post your comment

Please log in or register to post a comment.