Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
How is it possible that this is valid C++?
void main()
{
int x = 1["WTF?"];
}
On VC++10 this compiles and in debug mode the value of x
is 84 after the statement.
What's going on?
Array subscript operator is commutative. It's equivalent to int x = "WTF?"[1];
Here, "WTF?"
is an array of 5 char
s (it includes null terminator), and [1]
gives us the second char, which is 'T'
- implicitly converted to int
it gives value 84.
Offtopic: The code snippet isn't valid C++, actually - main
must return int
.
You can read more in-depth discussion here: In C arrays why is this true? a[5] == 5[a]
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