answersLogoWhite

0

What is an dimension?

User Avatar

Wiki User

โˆ™ 2013-04-29 13:52:54

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.

User Avatar

Darion Mohr

Lvl 9
โˆ™ 2022-12-08 06:48:28
This answer is:
User Avatar
Study guides

Algebra

20 cards

A polynomial of degree zero is a constant term

The grouping method of factoring can still be used when only some of the terms share a common factor A True B False

The sum or difference of p and q is the of the x-term in the trinomial

A number a power of a variable or a product of the two is a monomial while a polynomial is the of monomials

โžก๏ธ
See all cards
3.8
โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…
2032 Reviews

Add your answer:

Earn +20 pts
Q: What is an dimension?
Write your answer...
Submit
Still have questions?
magnify glass
imp
People also asked