int[5,5,5,5]

Brighton Beach (3)
Is it legal to declare multidimensional array like that?

int *a=new int[5,5,5,5];

It translated without an error.
Caligulaminus (171)
It translated without an error.

And int[5,5,5,5] translates to int[5].
Brighton Beach (3)
Thanks. I'm just wondering why did they do that? Very strange.
xerzi (570)
You can only pass one parameter to the operator[] function. It is actually read like this (because the comma is also an operator):

int *a=new int[ (5,5,5,5) ];

So the last value will be what it actually takes as a parameter.

To allocate a multi-dimension array you do it like this:

int (*a)[5] = new int[5][5];
Last edited on
Brighton Beach (3)
Thanks, xerzi.
vlad from moscow (3112)
int[5,5,5,5] is not a multidimensional array. It is a one-dimensional array the size of which is specified with an expression using the comma operator..
To declare a multidimensional array you should write

int[5][5][5][5];

andywestken (1950)
Also see the "Comma operator ( , )" section of

Operators
http://www.cplusplus.com/doc/tutorial/operators/

The comma operator is also known as the sequence operator.

Andy
Topic archived. No new replies allowed.