ProgrammingHardTCS NQT · Infosys pattern

What is printed (64-bit, 4-byte int)?

What is printed (64-bit, 4-byte int)?
int a[5] = {1,2,3,4,5};
int *p = a + 5;
printf("%d", *(p - 1) + *(a + 0));
  1. A. 6
  2. B. Undefined behavior: p points one past the end, dereferencing it is illegal
  3. C. 5
  4. D. 10

Correct answer: A. 6

a + 5 is a valid one-past-the-end pointer (legal to compute, just not to dereference directly). p - 1 brings it back to &a[4] (value 5), which IS valid to dereference. a[0] is 1. 5 + 1 = 6.

More C Programming questions

Exams that ask this topic