W
wintaki
Given
int x;
What does
x["123"] evaluate to?
If x is 0, it equals 0x31, the ascii value of '1'. So for some
reason, x[y] evaluates to *(y+x) where x is a non pointer/array type.
The same as if X was a pointer. But:
int x;
int *y;
x[0] -> error, pointer type required
y[0] -> obviously OK.
x["test"] or x[&x] -> OK
My question is why is this? I would think x[y] would be an error if x
was not a pointer/array. It seems as if the compiler does not care
and as long as one of the values in the expression x[y] is a pointer
and the other is a value, it transforms it into *(x+y). Is there a
reason for this? x[y] where x is a plain int makes no logical sense
to me.
Thanks for any explainations.
Wintakisan
int x;
What does
x["123"] evaluate to?
If x is 0, it equals 0x31, the ascii value of '1'. So for some
reason, x[y] evaluates to *(y+x) where x is a non pointer/array type.
The same as if X was a pointer. But:
int x;
int *y;
x[0] -> error, pointer type required
y[0] -> obviously OK.
x["test"] or x[&x] -> OK
My question is why is this? I would think x[y] would be an error if x
was not a pointer/array. It seems as if the compiler does not care
and as long as one of the values in the expression x[y] is a pointer
and the other is a value, it transforms it into *(x+y). Is there a
reason for this? x[y] where x is a plain int makes no logical sense
to me.
Thanks for any explainations.
Wintakisan