All mock tests are FREE till 31 December — register to save your progress

Consider the following code segment:
int arr[] = {0,1,2,3,4};
int i = 1, *ptr;
ptr = arr + 2;
Arrange the following printf statements in increasing order of their output:
(a) printf("%d", ptr[i]);
(b) printf("%d", ptr[i+1]);
(c) printf("%d", ptr[-i]);
(d) printf("%d", ptr[-i+1]);

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. (c), (d), (a), (b)

Explanation

ptr points to arr[2]. ptr[1] = 3, ptr[2] = 4, ptr[−1] = 1, ptr[0] = 2. Increasing: (c) 1, (d) 2, (a) 3, (b) 4.

More Programming Languages and Computer Graphics questions