1

What's the difference between declaring multidimensional arrays like this:

int a[5][5];

or this

int* a[5];

?

Which one is better to use? Thanks.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Rory Flast
  • 83
  • 6
  • You may find http://cdecl.org/ useful for 'decoding' complex C declarations. – nimrodm Nov 04 '11 at 06:19
  • 1
    possible duplicate of [Is int *array\[32\] a pointer to an array of 32 ints, or an array of 32 pointers to int? Does it matter?](http://stackoverflow.com/questions/1371898/is-int-array32-a-pointer-to-an-array-of-32-ints-or-an-array-of-32-pointers-t) – Brian Roach Nov 04 '11 at 06:20
  • A related question: http://stackoverflow.com/q/7586702/51831 – jpalecek Nov 04 '11 at 22:32

4 Answers4

1

Both may be used to declare 2-dimensional arrays.

In the first case, 25 int elements are allocated in a contiguous region in memory. In this case the expression a[i][j] is translated by the compiler to *(a + i*5 + j).

The second one allocates 5 pointers to int. You can make it work as a two dimensional array by allocating vectors of int and making these pointers point to these vectors. In this case a[i][j] means get the pointer that a[i] points to, then look up the 5th element in that vector. I.e. a[i][j] is translated to *(a[i] + j).

Note that in the second case, rows need not be of the same length.

nimrodm
  • 23,081
  • 7
  • 58
  • 59
0
  • The first one you are declaring bidimensional array (an array of arrays).
  • The second you're declaring an array of pointers to ints which can used as multidimensional array.

Is int *array[32] a pointer to an array of 32 ints, or an array of 32 pointers to int? Does it matter?

Take a look at http://www.cplusplus.com/doc/tutorial/arrays/ for declaring multidimensional arrays

Community
  • 1
  • 1
ob_dev
  • 2,808
  • 1
  • 20
  • 26
0

int a[5][5]; says that the compiler should allocate enough memory to store 25 ints, and to treat it as a two-dimensional array called a.

int* a[5]; says that the compiler should allocate enough memory to store 5 pointers to integers, and treat it as an array of pointers called a. To actually use this as a two-dimensional array, you will have to allocate more memory and initialize those pointers to point at it. For example:

int * a[5];
for(int i = 0; i < 5; i++){ a[i] = malloc(sizeof(int) * 5); }

Without knowing your requirements it's hard to say which one is better for you. The second one allows you to efficiently swap two rows and is generally more flexible, but it takes more code to allocate and free the data structure.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

Which is better apple or orange ? this notation int a[ROWS][COLS]is used to create rectangular 2D array which dimensions are known at declaration time. And this int * a[ROWS] notation is used when number of columns are unknown at 2D array declaration time OR number of columns can vary in run-time OR simply one wants to create non-rectangular (jagged) array like this:

1 2 3 4
4 6
7 1 1 1 5 6 7
9
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70