views:

290

answers:

3

Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]

3["zdvnngfgnfg"];

+8  A: 

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.

sharptooth
Doesn't the meaning also include "and then fetch the value at that address"?
unwind
I suppose no, since the statement is neither on the left nor on the right side of the assignment.
sharptooth
Since the snippet is a full expression, it does in fact mean "and then fetch the value". But because doing so has no defined side-effects, and the result is discarded, the compiler is allowed to omit it. In fact the compiler will probably omit the pointer addition as well - all it will do is make sure the statement is legal, realise it does nothing, and elide it. Certainly that's what gcc does, even with no optimisation.
Steve Jessop
Johannes Schaub - litb
Indexing a constant string makes for especially nice-looking code when used to convert to a hexadecimal digit: char digit = "0123456789ABCDEF"[i];
Heath Hunnicutt
+6  A: 

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!!

Neeraj
+2  A: 

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?

AraK