+ 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]);
4 Antworten
+ 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
+ 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 : )
+ 2
i think we can take (arr+3)[2] as arr[3+2]==> arr[5]==>'e', right??
+ 1
Ipang thanks a lot!, i newly know an array can be used like that way