473,401 Members | 2,139 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,401 software developers and data experts.

Question about some funky behavior with array subscripts

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

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

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

"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

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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz +...
7
by: Derek Basch | last post by:
I can remove objects from an array by doing this: for (i in oCache){ if (i == "test"){ delete oCache; }; }; However, the array's length property is unaffected.
20
by: Pavel Stehule | last post by:
Hello, Is possible merge two arrays like array + array => array select array_append(array, array); ERROR: function array_append(integer, integer) does not exist
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
23
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
10
by: Michael | last post by:
Hi, I'm trying to get my head around the relationship between pointers and arrays. If I have the following: int even = {2,4,6,8,10}; int *evenPtr = {even+4, even+3, even+2, even+1, even};...
3
by: Mike Cain | last post by:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0 C++ with the MFC CBuffer class. If I do this: #define NUM_ELEMENTS 2 CBuffer<char, 512 x; CBuffer<char, 512 ...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.