Array declaration and initialization, output array element. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Array declaration and initialization, output array element.

Enlighten fool please: int a[3]={2,1}; printf (“%d”, a[a[1]]); In this code is initialized an array by using a comma-separated list of values enclosed in braces. Where’s the third element? What does it a[a[1]] mean? Why the result of code execution is one?

7th Apr 2019, 4:07 PM
Caveman
Caveman - avatar
2 Answers
+ 3
array size can be larger than the initialized array, that's not a problem, the printf("%d", a[a[1]]); let break it down this way a[a[1]] - - >> will 1st check the inner part which is a[1] and this point to 1 which is also 1 with this we will now have a[1] which is also 1 again, so the output is 1
7th Apr 2019, 8:20 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Just remember that you have to calculate the bracket things first so In a[a[1]] lets write it this as b[c[1]] (remember b and c are same as 'a' i have written this just for demonstration purposes) so now in b[c[1]] you should calculate the things that are in bracket first so, c[1] = 1 (the second element in 'a') then b[c[1]] = b[1] = 1 (again the second element in 'a') And also for the question you can always make a program and try to print it in this way you will learn more
7th Apr 2019, 5:33 PM
Sahil Bhakat
Sahil Bhakat - avatar