Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

What is printed? (pointer arithmetic on an int array)

What is printed? (pointer arithmetic on an int array)
int a[] = {10, 20, 30, 40, 50};
int *p = a + 1;
printf("%d ", *(p + 2));
printf("%d ", *p++ + 2);
printf("%d", *p);
  1. A. 40 32 30
  2. B. 40 22 30
  3. C. 30 22 30
  4. D. 40 22 20

Correct answer: B. 40 22 30

p points to a[1] = 20. *(p + 2) is a[3] = 40. *p++ + 2 reads *p (20) then advances p, so it prints 22, and p now points to a[2] = 30. Postfix ++ binds tighter than *, but the dereference uses the old pointer value.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic