ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[5] = {1, 2, 3, 4, 5};
    int *p = a + 4;
    printf("%d %d", p[-2], *(p - 4));
}
  1. A. 5 1
  2. B. 3 2
  3. C. 4 1
  4. D. 3 1

Correct answer: D. 3 1

p points at a[4]=5. p[-2] is *(p-2), which is a[2]=3. *(p-4) is a[0]=1. Negative offsets from a pointer are valid as long as the result stays within the array.

More C Programming questions

Exams that ask this topic