Home | API | MFC | C++ | C | Previous | Next

Programming With C

Arrays

An array is a data structure consisting of a collection of elements (values or variables) of the same data type. Each storage location in an array is called an array element and is identified by an array index or key. The lowest index address corresponds to the first element and the highest index address to the last element.

Declare an array

The declaration statement for an array is similar to the one used to declare any other variable with the exception that the array name is followed by the number of elements within the array holds specified inside square brackets –

int example[5];

The example array (above) declares 5 sequential int integers. This declaration causes the compiler to set aside enough memory to hold all 5 elements. The number of an array element also is called it's subscript.

Initialising Arrays

Arrays can be initialised when they are first declared by use of the equal sign followed by a list of values enclosed in braces and separated by commas:

int example[5]= { 10, 20, 30, 40, 50 };

In the example above, the value 10 is assigned to example[0], the value 20 is assigned to example[1], the value 30 is assigned to example[2], the value 40 is assigned to example[3]  and the value 50 is assigned to example[4]. If the array size is omitted, the compiler creates an array just large enough to hold the initialisation values. The following statement would have exactly the same effect as the previous array declaration statement:

int example[] = { 10, 20, 30, 40, 50 }; 

Although the number of initialisation values can be fewer than the number of an array element, too many initialisers ie more initialisers than array elements will cause the compiler to generate an error. Any array element not initialised will contain an unknown value

multidimensional array

A multidimensional array has more than one subscript. A two-dimensional array has two subscripts and a three-dimensional array has three subscripts.  There is no limit to the number of dimensions a C array can have although the size of an array will be limited by the amount of memory on the computer. The following declaration for a two-dimensional array will be able to hold 64 int values

int example[8][8];

Multidimensional arrays can also be initialized by assigning array elements in order. For example:

int array[2][4] = { 1, 2, 3, 4, 5, 6, 7, 8 };

which can also be written

array[2][4]={{1,2,3,4},{5,6,7,8}};

results in the following assignments:

array[0][0]=1
array[0][1]=2
array[0][2]=3
array[0][3]=4
array[1][0]=5
array[1][1]=6
array[1][2]=7
array[1][3]=8

Accessing Data Stored in an Array

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. These indexes are called zero-based because the first element in an array is at index 0. The first integer value stored in an array arraydata[10] is arraydata[0], the second is arraydata[1], and so on. The index of the last element in an array is always (Length of Array – 1) or arraydata[9].


Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables | Constants | Expressions and Operators | Controlling Program Flow | Input & Output | C++ Functions | Arrays | Characters and Strings | String Manipulation | Pointers | Structures | Unions | File Input and Output | Advanced Pointers | Type Conversion | Dynamic Memory Allocation

Last Updates:25 October 2022