ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int a[3] = {1, 2, 3};
int *p = &a[1];
printf("%d %d", p[-1], p[1]);
  1. A. 2 3
  2. B. 1 3
  3. C. 3 1
  4. D. Undefined behaviour

Correct answer: B. 1 3

p points at a[1]. p[-1] is *(p - 1) = a[0] = 1 (valid, since it's still within the array's bounds). p[1] is *(p + 1) = a[2] = 3 (also valid). Output: 1 3.

More C Programming questions

Exams that ask this topic