+ 29

[SOLVED] Can Anyone Please Explain The Following C++ Program ??

/* C++ Program using strings */ int main() { char str[]="Test"; int len = strlen(str); for(int i=0;i<len;i++) { cout << str[i] << i[str] << *(str +i) << *(i + str) << endl; } } [ Output of the above program is coming as : TTTT eeee ssss tttt ] Like I am not able to get what is i [str] and that all.

20th Nov 2018, 12:54 PM
Nova
Nova - avatar
8 Answers
+ 5
i[str] == *(i + str) So basically you are doing the same as normal str[i]. The difference is you are checking the address of i first and and making a sum with first posittion of str after. Its same effect Like 2+4 = 6 4+2 = 6 You are just operating in reverse order.
20th Nov 2018, 4:39 PM
Anya
Anya - avatar
+ 16
Okay and can you also explain the working of i[str] ???
20th Nov 2018, 4:27 PM
Nova
Nova - avatar
+ 16
Ohhh Thank You Very Much For Your Help 😊
20th Nov 2018, 4:48 PM
Nova
Nova - avatar
+ 15
Anya Ok but how will the *(str + i) work ??
20th Nov 2018, 2:00 PM
Nova
Nova - avatar
+ 15
Keshav No 😅
26th Nov 2018, 8:17 AM
Nova
Nova - avatar
+ 6
str[ÂĄ] == *(str + i) i[str] == *(i + str) They all have same effect
20th Nov 2018, 1:16 PM
Anya
Anya - avatar
+ 5
Basically *(address) So *(str + i) str store first address position of array. And i it's the value to iterate. So *(str + 2) -> str[2] * means access the value in follow address.
20th Nov 2018, 3:54 PM
Anya
Anya - avatar
0
do u happen to be keval moliya?
25th Nov 2018, 6:03 PM
Keshav