Why is this snippet compilable in C?
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
3["zdvnngfgnfg"];
It's equivalent to
"zdvnngfgnfg"[3];
which is legal and means "take the address of that literal and add 3*sizeof(char)
to it". Will have no effect anyway.
Also see this very similar question.
arr[i] is parsed as *(arr+i) which can be written as *(i+arr) and hence i[arr]
Now "strngjwdgd" is a pointer to a constant character array stored at read only location.
so it works!!
The string literal( array
) decays to a pointer of type char*
. Then you take the fourth element:
3["zdvnngfgnfg"] == "zdvnngfgnfg"[3]
Why you can write the subscript infront of the array is another question:
In C arrays why is this true?
链接地址: http://www.djcxy.com/p/86720.html上一篇: C中的chars [4]和4 [chars]是否相同? 为什么?
下一篇: 为什么这个代码片段可以在C中编译?