Multiple choice

int y[4] = {6, 7, 8, 9}; int ptr = y + 2; printf(%dn, ptr[ 1 ] ); /*ptr+1 == ptr[1]/

What is printed, when the sample code above is executed?

  1. 6

  2. 7

  3. 8

  4. 9

  5. The code will not compile

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

ptr is assigned y+2, which points to the third element (index 2, value 8). ptr[1] means *(ptr+1), which accesses the next element (index 3, value 9). Array indexing with pointers works by adding the index to the base pointer and dereferencing.