ProgrammingHardGATE CS · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int vals[3] = {10, 20, 30};
    int *ptrs[3];
    ptrs[0] = &vals[2];
    ptrs[1] = vals;
    ptrs[2] = ptrs[0] - 1;
    printf("%d %d", *ptrs[2], *(ptrs[2] + 1));
}
  1. A. 30 20
  2. B. 20 30
  3. C. 10 20
  4. D. 20 20

Correct answer: B. 20 30

ptrs[2] is &vals[1], so the first value is 20 and the next element is vals[2] = 30. The pointer arithmetic stays inside the array.

More C Programming questions

Exams that ask this topic