I am dealing pointers in c and when i run the following code i get "l" as the output! Why?
char *s = "Hello, World!";
printf("%c", 2[s]);
What does 2[s] signify?
2[s]
is same as s[2]
because compiler convert both into *(2 + s)
here is a good link for you: why are both index[array] and array[index] valid in C?
This prints s[2] which is l. It's because s[i] is a syntactically equal to *(s + i). Therefore s[2] and 2[s] are both converted to *(s + 2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With