473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

what does [ ] mean with an integer?

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
Nov 14 '05 #1
6 3007

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.

Nov 14 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wintaki wrote:
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)

iD8DBQFB8QoNagVFX4UWr64RAjeyAKCeDMwLGUdBIOiCHWwmwo MqP+Wq9wCgxy68
dZXzn9xDYxTLdijwJKYWw+c=
=UfdS
-----END PGP SIGNATURE-----
Nov 14 '05 #3

wintaki wrote:
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"

Nov 14 '05 #4
wi*****@hotmail.com (wintaki) writes:
Given

int x;

What does

x["123"] evaluate to?

[snip]

This is question 6.11 in the C FAQ,
<http://www.eskimo.com/~scs/C-faq/faq.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
wintaki wrote:
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

Nov 14 '05 #6
Thomas Matthews <Th*************************@sbcglobal.net> writes:
wintaki wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
9
by: Thelma Lubkin | last post by:
I've been looking at code that handles string manipulation and I keep seeing variable names, and function names, too, followed by a '$'. I've also found a variable followed by a '%' symbol, but...
2
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
2
by: needin4mation | last post by:
I was looking at this code, and have seen it in other code (like it): /// <summary> /// Bitmap font class for XNA /// </summary> public class BitmapFont { private SpriteBatch m_sb; private...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
13
by: Protoman | last post by:
I'm getting an error: 10 C:\Dev-Cpp\Enigma.cpp no match for 'operator<' in 'i < (+cleartext)->std::basic_string<_CharT, _Traits, _Alloc>::end ()' Code: Enigma.hpp...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.