Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is dimension range of higher dimensions in multi-dimensional array required?

Tags:

c++

c

As per the post,

Passing a 2D array to a C++ function

int array[10][10];
void passFunc(int a[][10]) //  <---Notice 10 here
{
    // ...
}
passFunc(array);

Why is this higher dimension required from compilers internal point of view.

like image 570
Mahesh Attarde Avatar asked Feb 09 '16 10:02

Mahesh Attarde


People also ask

How many dimensions are possible in a multi-dimensional array?

More than Three Dimensions Although an array can have as many as 32 dimensions, it is rare to have more than three.

Can a multidimensional array have more than 3 dimensions?

Arrays can have more than one dimension. For example, the following declaration creates a two-dimensional array of four rows and two columns. int[,] array = new int[4, 2]; The following declaration creates an array of three dimensions, 4, 2, and 3.

What is the purpose of multidimensional array?

Multi-dimensional arrays are an extended form of one-dimensional arrays and are frequently used to store data for mathematic computations, image processing, and record management.

Can array have more than 2 dimensions?

The easiest way of understanding a multidimensional array is to acknowledge every array as a one dimensional array. i.e A 3 dimensional array is one dimensional array and every element in the one dimensional is a 2 dimensional array.


2 Answers

An alternative explanation (to array-to-pointer decay):

Let's say we have a one-dimensional array, and we use it like this:

int array[10];
int i = array[3];

The compiler has to know where to find array[3]. It knows it needs to skip 3 ints before it can get to the one in array[3]. So it works.

But if we have a two-dimensional array,

int array[2][5];
int i = array[1][1];

To get i here, how many ints does the compiler need to skip? It needs to skip an entire row, plus one. To skip one is easy, since we know the size of one int. But we also need to know the size of the row in the array—and the size of the row is determined by the size of the type * number of columns per row. This is one way of looking at it, which explains why you need the latter dimension.

Let's make this a small brain teaser by taking it one dimension further, to

int array[2][2][2];
int i = array[1][1][1];

and let's call the dimensions X, Y, Z.

Here, we can say we have a finite 3D space of ints. The unit is of course the size of one int. The number of rows is defined by Y, the number of planes is defined by Z. That leaves X as the basic unit, which is the size of one int, as we said. The combination of the three yields a "point."

To be able to get to any point in that 3D space, we need to know where each dimension "stops" and the next one begins. So we need:

  1. The size of the unit (int), to traverse the X dimension
  2. The size of each plane (Y), to traverse the Y dimension
  3. The number of planes, to traverse the Z dimension

So again, X is already given to us, because we're using int. But we don't know the size of each plane, nor do we know how many planes there are. So we need to specify all but the first dimension. And that's the general rule.

This also explains why this issue invites a bit more elaborate explanation than mere pointer decay, because once you get to more than 2 dimensions, you still need to know how this works. In other words, you need the overall size (product of dimensions) to not overflow, and you need the dimension of each size to be able to use successive [] indices.

like image 151
Yam Marcovic Avatar answered Sep 28 '22 09:09

Yam Marcovic


Arrays in C/C++ are a type, but not a first-class object and they "decay" into a pointer to the first element when passed to functions.

An int[10][10] is an array of 10 int[10] arrays... the function declarations:

void foo(int x[][10]);

typedef int IntArray10[10];
void bar(IntArray10 *x);

are for the compiler identical.

When passing a 2d array to a function therefore you're passing a pointer to the first element (and the first dimension is ignored) but the element itself is an array and the compiler needs to know its size.

like image 43
6502 Avatar answered Sep 28 '22 11:09

6502