what does [ ] mean with an integer?

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
 
J

john_bode

wintaki wrote:

[snip]
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).

That's it in a nutshell. The expression x[y] is defined to be exactly
equivalent to the expression *(x+y), so as long as one is a pointer and
one is an integer, it doesn't really matter which comes first. Since
the string literal "123" appears in a context that isn't a sizeof
operand or array initializer, it is treated as a pointer, so x["123"]
is equivalent to "123"[x] (which is also equivalent to a[x] or x[a],
where a is an array or pointer).

Yes, Virginia, array indexing in C is commutative. The first time I
pointed that out to a career Ada programmer, her head almost exploded.
Is there a reason for this?

dmr thought it was a cool idea? I don't know, it's just one of C's
many, many quirks.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Given

int x;

What does

x["123"] evaluate to?

If x is 0, it equals 0x31, the ascii value of '1'.

While this might be true in your specific implementation, in the general
case, the characterset is unknown, so the hex value is unknown. All that
is known is that the represented character, in this case, is '1', in the
execution characterset.
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.

Under the covers, the language treats all
x[y]
expressions as
*(x+y)

So, in your example of
x["ABC"]
the compiler interprets this as
*(x + "ABC")
(where "ABC" is actually a pointer to char)

If you had written the expression as
"ABC"[x]
the compiler would have interpreted it as
*("ABC" + x)
(again, where "ABC" is actually a pointer to char)

Since addition is commutative,
"ABC" + x
is the same as
x + "ABC"
and thus both expressions are equivalent.

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).

Exactly correct.
Is there a
reason for this? x[y] where x is a plain int makes no logical sense
to me.

Such are the quirks of pointer arithmetic.
Thanks for any explainations.

Wintakisan


- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB8QoNagVFX4UWr64RAjeyAKCeDMwLGUdBIOiCHWwmwoMqP+Wq9wCgxy68
dZXzn9xDYxTLdijwJKYWw+c=
=UfdS
-----END PGP SIGNATURE-----
 
S

sgerchick

wintaki said:
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

a[x] is equal to x[a], regardless of what x and a are. it is also
equal to *(a+x), like you said.

so, x["123"] is "123"[x]....we are looking at the literal "123" and
seing what is in position x. Printing it out in hex will yeild
whatever the encoding you use is for "1"
 
T

Thomas Matthews

wintaki said:
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

This could be C++ your are looking at.
The C++ has a map (associative array) data structure that can
be accessed like an array.
std::map<key_type, value_type> my_map.

It is possible to have the key as a string. The statement could
be coded as:
std::map<char *, int> x;
/* ... */
int y;
y = x["123"];
In the above statement, the variable 'y' is assigned to
the value in the map, 'x', which is associated with the
key "123". In other words, the map contains a record:
<"123", value>
and the expression:
x["123"]
returns the 'value' field or component.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Keith Thompson

Thomas Matthews said:
wintaki said:
Given
int x;
What does
x["123"] evaluate to?
[...]
This could be C++ your are looking at.
The C++ has a map (associative array) data structure that can
be accessed like an array.
std::map<key_type, value_type> my_map.

It might be C++, but it's not a std::map. The OP already said that x
is declared as int.

C FAQ 16.11.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top