While I was learning Strings in C I came across the following piece of code
#include <stdio.h>
int main()
{
char str[15] = "Hello, World";
printf("str[0] = %c\n", str[0]);
printf("*(str + 0) = %c\n", *(str + 0));
printf("*(0 + str) = %c\n", *(0 + str));
printf("0[str] = %c\n", 0[str]); /* This line 0[str] */
return 0;
}
Simple things what I can figured out from the program are
str[0]
will produce H
*(str + 0)
will also produce H as it uses pointer arithmetic
*(0 + str)
is similar to *(str + 0)
and will also produce H
What I can't figure out is what does 0[str]
means and how does it evaluates to H. As what I have learnt is [ ]
symbol represents an array and 0
is not an array.