GATE | GATE IT 2006 | Question 51

Which one of the choices given below would be printed when the following program is executed?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
            printf("%d,", a[0][2]);
            printf("%d,", *a[2]);
            printf("%d,", *++a[0]);
            printf("%d,", *(++a)[0]);
            printf("%d\n", a[-1][+1]);
}
main()
{
             print(x);
}

chevron_right


(A) 8, -12, 7, 23, 8
(B) 8, 8, 7, 23, 7
(C) -12, -12, 27, -31, 23
(D) -12, -12, 27, -31, 56


Answer: (A)

Explanation:  

int *x[] = {a1, a2, a3}; line makes x[0] = a1 and likewise. Therefore whenever x[0] is referenced, internally a1 array is being looked upon.

a[0][2] = a1[2] = 8

*a[2] = *a3 = a3[0] = -12



*++a[0] = *(++a[0]) = a1[1] = 7 : a[0] is now pointing to second element of a1.

*(++a)[0] = *a2 = a2[0] = 23 : a is now pointing to array a2.

a[-1][+1] = *(a1+1+1) = a1[2] = 8 : *(a-1) = a1+1 due to last two expressions.

Hence, correct answer would be (A) 8, -12, 7, 23, 8.

This solution is contributed by vineet purswani .

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.