Question about some funky behavior with array subscripts

Discussion in 'C++' started by Pete, Nov 13, 2003.

  1. Pete

    Pete Guest

    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
     
    Pete, Nov 13, 2003
    #1
    1. Advertising

  2. Pete

    Ron Natalie Guest

    "Pete" <> wrote in message news:...
    > 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
     
    Ron Natalie, Nov 13, 2003
    #2
    1. Advertising

  3. Pete

    Josh Lessard Guest

    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[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
    *****************************************************
     
    Josh Lessard, Nov 13, 2003
    #3
  4. Hi Pete,

    "Pete" <> schrieb im Newsbeitrag
    news:...
    > 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.
     
    Ekkehard Morgenstern, Nov 13, 2003
    #4
  5. Pete

    Ron Natalie Guest

    "Ekkehard Morgenstern" <> wrote in message news:bp0v8e$b2b$...

    > 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?
     
    Ron Natalie, Nov 13, 2003
    #5
  6. Hi Ron,

    "Ron Natalie" <> schrieb im Newsbeitrag
    news:3fb40179$0$25268$...
    >
    > 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.
     
    Ekkehard Morgenstern, Nov 13, 2003
    #6
  7. Pete

    lilburne Guest

    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.
     
    lilburne, Nov 13, 2003
    #7
  8. Pete

    Pete Guest

    > 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
     
    Pete, Nov 14, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cameron, Charles B.

    VHDL Subscripts

    Cameron, Charles B., Feb 26, 2004, in forum: VHDL
    Replies:
    2
    Views:
    650
    Cameron, Charles B.
    Feb 26, 2004
  2. Replies:
    25
    Views:
    710
    George Sakkis
    Jun 11, 2006
  3. order of array subscripts

    , Mar 25, 2008, in forum: C Programming
    Replies:
    9
    Views:
    377
  4. E.M.Smith

    Funky FormsAuthentication Cookie Behavior.

    E.M.Smith, Jun 2, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    226
    Alex Kleyman
    Jun 3, 2004
  5. lloyd
    Replies:
    2
    Views:
    376
    lloyd
    Nov 5, 2011
Loading...

Share This Page