Question about some funky behavior with array subscripts

P

Pete

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[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[j]) << endl;
cout << &(ary[i,j]) << endl;

// END FAKEY-CODE
//
//-------------------------


Curious,


Pete
 
R

Ron Natalie

Pete said:
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
 
J

Josh Lessard

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[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
*****************************************************
 
E

Ekkehard Morgenstern

Hi Pete,

Pete said:
I know that C/C++ does not have two-dimensional arrays, thus
ary[i,j] is not valid notation. Instead I should use ary[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[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]; }" (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.
 
R

Ron Natalie

Ekkehard Morgenstern said:
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]; }" (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?
 
E

Ekkehard Morgenstern

Hi Ron,

Ron Natalie said:
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.
 
L

lilburne

Ron said:
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.
 
P

Pete

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top