ProgrammingModerateTCS NQT · Accenture

What is printed?

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

Correct answer: D. 2 5

p points at a[2], so p[-1] is a[1] = 2 and p[2] is a[4] = 5. Forgetting the offset from the pointer base gives 1 4.

More C Programming questions

Exams that ask this topic