+ 4

Can somebody please explain why the output 123?

int a[]={1,2,3}; int i; for(i=0;i<3;i++) { cout<< i[ a ]; }

19th Apr 2017, 2:04 PM
Aniket Singh
7 Réponses
+ 15
I[a] is similar to a[I] . As array use contiguous memory allocation, both of them is evaluated to *(a+I) *a = a[0]. *(a+1) = a[1]
19th Apr 2017, 2:58 PM
manoj katari
manoj katari - avatar
+ 7
Answer is Syntax Error
19th Apr 2017, 2:04 PM
Pixie
Pixie - avatar
+ 7
Hi Aniket! I read that question as one containing a typo. As such, cout << a[i]; Edit: Thanks for the clarification!
19th Apr 2017, 2:23 PM
AtK
+ 1
Aniket good question Manoj thanks for the great explanation i have run this program, as Aniket said its giving output 123 so it is not a typo or syntax error Manoj's explanation is the only one that fits here. Thank you
19th Apr 2017, 4:56 PM
Jilson Joseph
+ 1
tnnx Manoj ... Great work
19th Apr 2017, 5:33 PM
Aniket Singh
+ 1
because you d'ont put endl; end line or any caracter to separate the output.
20th Apr 2017, 2:41 PM
‏‪‏‪عبدالله بوستة‬‏
‏‪‏‪عبدالله بوستة‬‏ - avatar
0
no but I have run it .. the output is 123
19th Apr 2017, 2:16 PM
Aniket Singh