How this code works? for me it looks complicated and i still don't understand, the output is "83 e" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How this code works? for me it looks complicated and i still don't understand, the output is "83 e"

char str[] = "%d %c", arr[] = "Sololearn"; printf (str, 0[arr], 2[arr + 3]);

21st Feb 2019, 5:21 PM
Okta Alfiansyah
Okta Alfiansyah - avatar
4 Answers
+ 11
0[arr] equals to arr[0] This points to offset zero (character 'S'). The ASCII code for character 'S' is 83, and this is what we get because we use `%d` (int) format specifier for that value. 2[arr + 3] equals to (arr + 3)[2] First, we adjust the pointer three bytes ahead, and by that, it points to offset 3 (second character 'o'). From this offset we look down two bytes, and arrived at offset 5 (character 'e'). And that's what we get because we use `%c` (char) format specifier for that value. --> arr | --> (arr + 3)[0] | | --> (arr + 3)[2] | | | offset 0 1 2 3 4 5 6 7 8 value S o l o L e a r n Hth, cmiiw
21st Feb 2019, 6:40 PM
Ipang
+ 8
You're welcome Okta Alfiansyah and it is possible because internally an array is a pointer that points to a memory location containing sequence of value of a certain type, in this case a sequence of character. I hope I don't confuse you, because I'm still learning this : )
21st Feb 2019, 7:04 PM
Ipang
+ 2
i think we can take (arr+3)[2] as arr[3+2]==> arr[5]==>'e', right??
14th Oct 2020, 6:46 AM
mohana hemanth
mohana hemanth - avatar
+ 1
Ipang thanks a lot!, i newly know an array can be used like that way
21st Feb 2019, 6:57 PM
Okta Alfiansyah
Okta Alfiansyah - avatar