answersLogoWhite

0

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

Wiki User

8y ago

What else can I help you with?

Related Questions

What is the array of string in c?

A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.


What are the kinds of arrays?

1. One dimension array 2. Two dimension array 3. Multi dimentional array


What is single dimentional?

A single dimension array is an array with one dimension. It is a collection in memory of one or more elements of the same type. int array[100]; declares an array of int's of size 100 elements. The elements are referenced as array[0], the first one, through array[99], the last one.


Write a program to substrate two matrixs in two dimension array?

You need to specify the language in which you want the subtraction of the two matrix in two dimension array written.


What is single dimension in data structure?

Array, Stake, Queue.


Is it necessary to specify dimension while declaring an array?

yes


What is ragged array in java?

is an array with more than one dimension each dimension has different size ex: 10 20 30 11 22 22 33 44 77 88


What is the use of dim in one dimensional and two dimensional in array?

if one dimension means it have only rows but in two dimension means it have both row and coloumns


Define the declare statement for multi-dimension array?

<storage_class> <type> <identifer> '[ '<number1> ']' '[ '<number2> ']' ... ';'


How you pass array elements to a function?

Passing array elements to a function is achieved by passing the individual elements by reference or by value, just as you would any other variable. However, passing the entire array requires that you pass a pointer-to-pointer to the array along with the dimension(s) of the array.


What does it mean for a subscript to be out-of -bounds?

This is a type of error that usually occurs in computer programs. An array is defined in which the elements of the array are identified by one or more subscripts. Suppose you have an array which is declared to be of dimension 23. Then if the program tries to access element 26 in that array, it cannot because there is no element of the array in that position. That is when you will get this error message.


What is an array of 5 x 46?

An array is a contiguous memory allocation divided into one or more elements of equal size. A 5 x 46 array is an array of 5 elements where each element is another array of 46 elements. In other words it is an array of arrays. We can the array a two-dimensional array because it has 5 elements in one dimension (the rows) and 46 in the other dimension (the columns). If an individual column element is 4 bytes long, then each row element consumes 46 x 4 = 184 bytes of memory while the entire array consumes 5 x 184 = 920 bytes in total. We can also think of the entire array as being a one-dimensional array of 5 x 46 = 320 elements of 4 bytes each.