Multidimensional Arrays Переход на главную страницу

Multidimensional Arrays

An array may have more than one dimension (i.e., two, three, or higher). The organization of the array in memory is still the same (a contiguous sequence of elements), but the programmer’s perceived organization of the elements is different. For example, suppose we wish to represent the average seasonal temperature for three major Australian capital cities (see Table below).

Spring Summer Autumn Winter

This may be represented by a two-dimensional array of integers:

int seasonTemp[3][4];

The organization of this array in memory is as 12 consecutive integer elements:

As before, elements are accessed by indexing the array. A separate index is needed for each dimension. For example, Sydney’s average summer temperature (first row, second column) is given by seasonTemp[0][1].

The array may be initialized using a nested initializer:

int seasonTemp[][4] = {

{26, 34, 22, 17},

{24, 32, 19, 13},

{28, 38, 25, 20}


As you may see from initializer above we can omit the first dimension (but not subsequent dimensions).

Processing a multidimensional array is similar to a one-dimensional array, but uses nested loops instead of a single loop. Listing 2 illustrates this by showing a function for finding the highest temperature in seasonTemp.

Listing 2

const int rows = 3;

const int columns = 4;

int seasonTemp[rows][columns] = {

{26, 34, 22, 17},

{24, 32, 19, 13},

{28, 38, 25, 20}


int HighestTemp (int temp[rows][columns]) {

int highest = 0;

for (register i = 0; i < rows; ++i)

for (register j = 0; j < columns; ++j)

if (temp[i][j] > highest)

highest = temp[i][j];

return highest;



A pointer is simply the address of a memory location and provides an indirect way of accessing data in memory. A pointer variable is defined to ‘point to’ data of a specific type. For example:

int *ptr1; // pointer to an int

char *ptr2; // pointer to a char

The value of a pointer variable is the address to which it points. For example, given the definitions

int num;

we can write:

ptr1 = #

The symbol & is the address operator; it takes a variable as argument and returns the memory address of that variable. The effect of the above assignment is that the address of num is assigned to ptr1. Therefore, we say that ptr1 points to num.

Given that ptr1 points to num, the expression *ptr1 dereferences ptr1 to get to what it points to, and is therefore equivalent to num. The symbol * is the dereference operator; it takes a pointer as argument and returns the contents of the location to which it points.

In general, the type of a pointer must match the type of the data it is set to point to. A pointer of type void*, however, will match any type. This is useful for defining pointers which may point to data of different types, or whose type is originally unknown. A pointer may be cast (type converted) to another type. For example,

ptr2 = (char*) ptr1;

converts ptr1 to char pointer before assigning it to ptr2.

Regardless of its type, a pointer may be assigned the value 0 (called the null pointer). The null pointer is used for initializing pointers, and for marking the end of pointer-based data structures (e.g., linked lists).