ProgrammingModerateProduct-company MCQ pattern

With 4-byte int, what is printed?

With 4-byte int, what is printed?
int a[] = {1, 2, 3};
char *p = (char *)a;
p += 4;
printf("%d", *(int *)p);
  1. A. A garbage value
  2. B. 3
  3. C. 1
  4. D. 2

Correct answer: D. 2

As a char pointer, p += 4 moves 4 bytes, exactly one int, to the start of a[1]. Casting back to int * reads a[1] = 2. Had p stayed an int *, += 4 would have moved 16 bytes.

More C Programming questions

Exams that ask this topic