# c++ [] operator with multiple parameters

## Recommended Posts

Hi all, I've just made a typo that AFAIK should have been catched by the compiler but it wasn't. I tried to access to a float[][] by using values[2,1] instead than values[2][1]. The program didn't worked as expected, of course, but no errors were reported by the compiler of the linker (Visual c++ 2005). What does this operator use mean? Thank you!

##### Share on other sites
comma operator.

values[2,1] == values[(2,1)] == values[1]

##### Share on other sites
It's not operator[], but operator,, which is used here in a manner which is equivalent to:
int temp = (2,1);values[temp]

Where a,b, in C and C++, first evaluates a, and then evaluates and is equal to the value of b. In your case, since a is 2 and thus has no side-effect, your code is in fact accessing values[1].

##### Share on other sites
The comma (,) is an operator by itself (which you can overload as well). The default operation of the comma operator is to return its second argument. So the access to the array by array[2,1] will most likely be equivalent to array[1].
AFAIK the [] operator can only take one argument, but I may be wrong with this.

Edit: Damn it! Two were faster than me

##### Share on other sites
Me stupid! I always repeat to myself to never stay away from c++ for more than a couple month :-)
Yep, and most probably this is not even the only thing I forgot (templates syntax being another one :-(

Thank you!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627639
• Total Posts
2978344

• 10
• 12
• 22
• 13
• 33