C Quiz – 102 | Question 5

In the context of the below program snippet, pick the best answer.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include "stdio.h"
int arr[10][10][10];
int main()
{
 arr[5][5][5] = 123;
 return 0;
}

chevron_right


Which of the given printf statement(s) would be able to print arr[5][5][5]

filter_none

edit
close

play_arrow

link
brightness_4
code

(i) printf("%d",arr[5][5][5]);
(ii) printf("%d",*(*(*(arr+5)+5)+5));
(iii) printf("%d",(*(*(arr+5)+5))[5]);
(iv) printf("%d",*((*(arr+5))[5]+5));

chevron_right


(A) only (i) would compile and print 123.
(B) both (i) and (ii) would compile and both would print 123.
(C) only (i), (ii) and (iii) would compile but only (i) and (ii) would print 123.
(D) only (i), (ii) and (iii) would compile and all three would print 123.
(E) all (i), (ii), (iii) and (iv) would compile but only (i) and (ii) would print 123.
all (i), (ii), (iii) and (iv) would compile and all would print 123.


Answer:

Explanation: For arrays, we can convert array subscript operator [] to pointer deference operator * with proper offset. It means that arr[x] is equal to *(arr+x). Basically, these two are interchangeable. The same concept can be applied in multi-dimensional arrays as well.

That’s why all of the above 4 printf are referring to the same element i.e. arr[5][5][5]

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


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