brucezepplin
brucezepplin

Reputation: 9792

C array of arrays

Just trying to get my head around an array structure I need for a fitness function. It is an array of arrays:

double fitness[][2][9][11]

just from my understanding a [2][9][11] array would be a 2 by 9 by 11 3 dim array. That's fine, but the [] is throwing me off. How does this contribute to the array structure? Could someone give me an example input to this (or perhaps with a lot smaller dimensions as the array I have posted up would we quite large).

Thanks very much.

edit: this is within a function

Upvotes: 4

Views: 20151

Answers (2)

cpowel2
cpowel2

Reputation: 605

Essentially it is not necessary for it to be known how many rows are in the array. The reason that dimensions are declared in the first place is so that the computer is able to access a memory location referred to by indice in 2-D arrays it uses the following (Not sure for 4-D arrays you could look it up)

Address = Base + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size

so here is a small example say we have the following

int myArray[2][2];
/* put some values in the array */
myFunc(myArray);

then we go to access it in a function like

void myFunc(int array[][2])
{
     printf("%d\n", &array[1][1]);
}

essentially the address of array[1][1] which corresponds to myArray[1][1] is calculated using the formula above we go to that address read 4-bytes interpret it as an integer and print it to the screen. The reason the function does not care about how many rows are in the array is that C does not do any bounds checking it only needs to know how to calculate the memory address of whatever indices you give it using the formula if you look at the formula the number of rows is not needed (row size is based on number of columns) so C is perfectly happy with having you omit the number of rows in the method declaration since it can compute memory address without that information

Upvotes: 3

Jens
Jens

Reputation: 72746

It will become a little clearer if you reduce the number of dimensions. Let's be bold, remove all but one dimension, and declare

 extern double foo[];

What this does is declare an incomplete type, an array of an unknown number of doubles. The type would be complete if you gave an explicit number of elements. The difference is that you can't apply the sizeof to incomplete types: the compiler can't know their size because they are, well, incomplete.

Now if we declare an array of arrays we can't have both dimensions be unknown (like in double foo[][]). Why is that? Previously the compiler could compute the location of foo[42] because it knows the size of a double (because double is a complete type). To compute foo[1][42] it must know the size of the element type. In other words, element types must be complete. In C the rightmost dimension varies fastest, so the element type here is given by all but the leftmost dimension: double [42] and in your original question it is double [2][9][11].

At the end of the day, you can leave the leftmost dimension blank if you don't need the size of the complete array.

Upvotes: 3

Related Questions