By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

Question about some funky behavior with array subscripts

P: n/a
Quick question: What does ary[i,j] return in C/C++?

I know that C/C++ does not have two-dimensional arrays, thus
ary[i,j] is not valid notation. Instead I should use ary[i][j]
. But.
But I was this past week using g++, the GNU C++ compiler, running a
default install of Red Hat 9 (I don't know the exact compiler version,
but you get the gist). I made the mistake of using ary[i,j] in
my code, and the program *COMPILED WITHOUT ERRORS*. The resulting
behavior at run-time was, of course, wrong. Why? Why did it compile
in the first place?

What does ary[i,j] mean? It compiles, so it must mean something...
As an example, let's start with this:

//-------------------------
//
// BEGIN FAKEY-CODE

int ary[5][12];
int i = 4;
int j = 7;

cout << &(ary[i][j]) << endl;
cout << &(ary[i,j]) << endl;

// END FAKEY-CODE
//
//-------------------------
Curious,
Pete
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Pete" <ps********@zombieworld.com> wrote in message news:98**************************@posting.google.c om...
Quick question: What does ary[i,j] return in C/C++?


, is an operator. It evaluates the expression to the left, and then evaluates the
expression to the right (there's a sequence point between them) and the value of
the expression is the result of evaluating the right side.

a[i,j] is effectively the same as a[j]

G++ will warn you if you crank the warning level up.
warning: left-hand operand of comma expression has no effect

Jul 19 '05 #2

P: n/a
On 13 Nov 2003, Pete wrote:
Quick question: What does ary[i,j] return in C/C++?

I know that C/C++ does not have two-dimensional arrays, thus
ary[i,j] is not valid notation. Instead I should use ary[i][j]
. But.
But I was this past week using g++, the GNU C++ compiler, running a
default install of Red Hat 9 (I don't know the exact compiler version,
but you get the gist). I made the mistake of using ary[i,j] in
my code, and the program *COMPILED WITHOUT ERRORS*. The resulting
behavior at run-time was, of course, wrong. Why? Why did it compile
in the first place?

What does ary[i,j] mean? It compiles, so it must mean something...


Look up the comma (,) operator in your text. The comma operator evaluates
each of its expressions from left to right, and returns the value of the
rightmost expression. Thus, in essence, ary[i,j] does the same thing as
ary[j] (assuming i is an actual variable rather than a statement that
produces side effects).

************************************************** ***
Josh Lessard
Master's Student
School of Computer Science
Faculty of Mathematics
University of Waterloo
(519)888-4567 x3400
http://www.cs.uwaterloo.ca
************************************************** ***

Jul 19 '05 #3

P: n/a

Hi Pete,

"Pete" <ps********@zombieworld.com> schrieb im Newsbeitrag
news:98**************************@posting.google.c om...
I know that C/C++ does not have two-dimensional arrays, thus
ary[i,j] is not valid notation. Instead I should use ary[i][j]
. But.

What does ary[i,j] mean? It compiles, so it must mean something...
As an example, let's start with this:
int ary[5][12];
cout << &(ary[i][j]) << endl;
cout << &(ary[i,j]) << endl;


As the others pointed out already, the comma operator returns "j" in the
"i,j" expression in this case.

However, you could define an operator [] that takes two arguments instead of
one, so implementing two-dimensional arrays in C++ with comma-separated
arguments is indeed possible.

Try this: define a function "int operator[]( int a, int b ) { return
ary[a][b]; }" (in global scope, and provided ary is global).

In classes, this is often used to define two-dimensional arrays or other
behaviours, as in:

template < class T >
class TwoDimArray {
T** buf;
...
T& operator[]( int x, int y ) { return buf[y][x]; }
...
};

I hope that helps.

Regards,
Ekkehard Morgenstern.

Jul 19 '05 #4

P: n/a

"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:bp**********@online.de...
However, you could define an operator [] that takes two arguments instead of
one,
No you can't.
so implementing two-dimensional arrays in C++ with comma-separated
arguments is indeed possible.
There's no such thing as a 2d array in C++. An array is 1d. int a[10][20]
is a ten element array of twenty element arrays of ints.
Try this: define a function "int operator[]( int a, int b ) { return
ary[a][b]; }" (in global scope, and provided ary is global).
It's ill-formed. operator[] must be a non-static member function with exactly
one argument.
In classes, this is often used to define two-dimensional arrays or other
behaviours,


No it doesn't, it's not valid code. Where do you get this nonsense?
Jul 19 '05 #5

P: n/a

Hi Ron,

"Ron Natalie" <ro*@sensor.com> schrieb im Newsbeitrag
news:3f***********************@news.newshosting.co m...

No you can't.

It's ill-formed. operator[] must be a non-static member function with exactly one argument.

No it doesn't, it's not valid code. Where do you get this nonsense?


From my memory. Sorry, you're right. I was actually using operator() not
operator[].

Regards,
Ekkehard Morgenstern.
Jul 19 '05 #6

P: n/a
Ron Natalie wrote:

G++ will warn you if you crank the warning level up.
warning: left-hand operand of comma expression has no effect


Crank the warning level up as high as you can and turn them
all into errors.

Jul 19 '05 #7

P: n/a
> What does ary[i,j] mean? It compiles, so it must mean something...

Thanks all for the replies as to the comma operator, which seems
obvious now. It definitely wasn't obvious a while ago. Also, thanks
for the tips about the warning levels.
Pete
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.