ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int x = 5;
void *vp = &x;
int *ip = (int*)vp;
(*ip)++;
printf("%d", *(int*)vp);
  1. A. 5
  2. B. 6
  3. C. Compilation error: cannot dereference void*
  4. D. Undefined behavior, value unpredictable

Correct answer: B. 6

vp and ip both hold the address of x. Incrementing through ip modifies x to 6; reading back through a cast void* reads the same memory, so 6 is printed. void* itself cannot be dereferenced directly, but casting it to int* first is legal and exactly what both expressions do.

More C Programming questions

Exams that ask this topic