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

Discussion

What is the output of this C code?
 

int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

 

  • A.Same address
  • B.Different address
  • C.Compile time error
  • D.Varies

Answer: A

Correct answer: Same address

Incrementing x changes the value stored at the address, not the address itself, and ptr was never modified. So both %p conversions print the same thing. The question is testing that a pointer and the object it points to are separate: assigning through the pointer alters one, assigning to the pointer alters the other.

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

Post your comment

Please log in or register to post a comment.