Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question about some funky behavior with array subscripts

Reply
Thread Tools

Question about some funky behavior with array subscripts

 
 
Pete
Guest
Posts: n/a
 
      11-13-2003
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-13-2003

"Pete" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Josh Lessard
Guest
Posts: n/a
 
      11-13-2003
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
************************************************** ***

 
Reply With Quote
 
Ekkehard Morgenstern
Guest
Posts: n/a
 
      11-13-2003

Hi Pete,

"Pete" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) 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.



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-13-2003

"Ekkehard Morgenstern" <(E-Mail Removed)> wrote in message news:bp0v8e$b2b$(E-Mail Removed)...

> 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?


 
Reply With Quote
 
Ekkehard Morgenstern
Guest
Posts: n/a
 
      11-13-2003

Hi Ron,

"Ron Natalie" <(E-Mail Removed)> schrieb im Newsbeitrag
news:3fb40179$0$25268$(E-Mail Removed) 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.


 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      11-13-2003
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.

 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      11-14-2003
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
negative subscripts for 2D array of floats lloyd C Programming 2 11-05-2011 08:15 PM
order of array subscripts noway@jose.net C Programming 9 03-29-2008 05:10 AM
Funky behavior with dual monitor setup Slack Computer Support 2 08-14-2006 05:41 AM
Funky FormsAuthentication Cookie Behavior. E.M.Smith ASP .Net Security 1 06-03-2004 05:51 PM
VHDL Subscripts Cameron, Charles B. VHDL 2 02-26-2004 03:07 PM



Advertisments