Best Answer

An array dimension tells us the number of elements in an array:

int x[50]; // an array of 50 integer elements

Array elements are anonymous so we cannot refer to them by name. The array name, x, refers to the start of the array and thus to the first element in the array but it's important to realise that x really is a reference and not a named variable:

x = 42; // error! cannot assign a value to a reference

In order to assign a value to an array element you must first dereference that element:

*x = 42;

The first element of an array is always offset 0 elements from the start of the array, so we can also use pointer arithmetic to dereference the address:

*(x+0) = 42; // assign to the 1st element

By changing the offset index we can refer to any element in the array:

*(x+10) = 42; // assign to the 11th element

Dereferencing array elements using pointer arithmetic is a bit verbose and cumbersome, however we can use the array suffix operator to make things a little easier for both the programmer and the reader:

x[0] = 42;

x[10] = 42;

As far as the C compiler is concerned, x[0] really means *(x+0). The pointer arithmetic still occurs Behind the Scenes, but it is an implementation detail that is of no real concern to the programmer. However, in order to understand how arrays work, it is important we understand the underlying pointer arithmetic.

Note that the array suffix operator should not be confused with the array declarator:

int x[50]; // declarator

x[10] = 42; // suffix operator

The declarator tells the compiler how many elements of the given type to allocate (the dimension), while the suffix operator tells the compiler which element to dereference.

The notion of dimensions can be extended to create multi-dimensional arrays.

int y[5][10]; // two-dimensional array

Two-dimensional arrays are best thought of as being one-dimensional arrays of one-dimensional arrays. Here, y is a one-dimensional array of type int[10]. We can also think of them as being a table of rows and columns where every row is an array. The dimensions therefore tell us how many rows and columns there are in the table.

We can extend this line of thinking into three dimensions:

int z[3][4][5]; // three dimensional array

Here we have an array of 3 tables, where every table has 4 rows and each row has 5 elements of type int. Although we can imagine such an array as being a cuboid or as a stack of tables, it's always much easier to think one-dimensionally as it helps our understanding of arrays with more than 3 dimensions.

In this case it is better to think of z as being an array of 3 elements of type int[4][5], and each of those arrays as being an array of 4 elements of type int[5]. Extending the notion into 4 or more dimensions then becomes trivial:

int m[2][3][4][5];

Here, m is a one-dimensional array of 2 elements of type int[3][4][5].

Note that the C Programming language is a row-major language, thus with multi-dimensional arrays the row always comes first. In other words, m is a one-dimensional array of two rows, where every row is an array of type int[3][4][5]. Similarly, int[3][4][5] is a one-dimensional array of 3 rows of type int[4][5]. And so on.

Array elements are always allocated contiguously regardless of the number of dimensions. To determine the size of an array allocation in elements we simply multiply the dimensions. Thus m has sufficient memory to store 2*3*4*5=120 elements. To determine the length in bytes, we multiply the product of the dimensions by the size of the array type. Thus m is 120*sizeof(int) bytes in length.

Study guides

☆☆

Q: What is an dimension?

Write your answer...

Submit

Still have questions?

Related questions

Driving Dimension: the geometry is controlled by the dimension. Driven Dimension: the dimension is controlled by the geometry.

it has no dimension

pressure dimension

THere was a dimension in my classroom

dimension?

The fourth dimension.

dimension

For putting to many dimension on top of the dimension!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

The 2 dimension is times 2 but if it is 3 dimension is times 3

Yes there are such things of dimension.

The dimension is [L].

People also asked