Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

a question on two dimensional array.

woshiwuxin
Novice
279 Views
Hi, all!

I declared a two dimensional array.

int a[2][3];

I just expected 'a' is a type of 'int **'. However, icc gave me the info that 'a' is a type of 'int (*)[3]'.

I'm confused. What's the meaning of 'int (*)[3]'? How could 'a' be of that type?

Thanks in advance!
0 Kudos
1 Reply
Andreas_Klaedtke
Beginner
279 Views
Hi,

Have a look at:
http://donchel.wordpress.com/2010/10/28/multidimensional-arrays-and-pointer/

The C standard apparently says this:
6.5.2.1 Array subscripting
Constraints

3 Successive subscript operators designate an element of a multidimensional
array object.
If E is an n-dimensional array (n >= 2) with dimensions i * j * . . . * k,
then E (used a s other than an lvalue) is converted to a pointer to an
(n - 1)-dimensional array with dimensions j * . . . * k.
If the unary * operator is applied to this pointer explicitly, or
implicitly as a result of subscripting, the result is the pointed-to
(n - 1)-dimensional array, which itself is converted into a pointer if
used as other than an lvalue. It follows from this that arrays are stored
in row-major order (last subscript varies fastest).
Together with int * is not identical to int [], this is what the compiler is telling you, I think.

My impression is that the allocation scheme for multidimensional static arrays is not defined in the C standard. The assumption that int a[2][3] is equivalent to 2 * 6 = 6 ints in a sequence in memory is usually correct (I have tested this with the gcc and icpc compilers), but must not hold in every case.
I think that this might have something to do with the fact that a is not of type int**, but int (*)[3] in these cases.

But casting the pointer a or a[0] to an int * does work and produces the right results.

I hope this is of help to you.
0 Kudos
Reply