Array definition
Arrays are used in C to represent structures of consecutive elements of the same type. The definition of a (fixed-size) array has the following syntax:
int array[100];
which defines an array named array to hold 100 values of the primitive type int. If declared within a function, the array dimension may also be a non-constant expression, in which case memory for the specified number of elements will be allocated. In most contexts in later use, a mention of the variable array is converted to a pointer to the first item in the array. The sizeof operator is an exception: sizeof array yields the size of the entire array (that is, 100 times the size of an int, and sizeof(array) / sizeof(int)
will return 100). Another exception is the & (address-of) operator, which yields a pointer to the entire array, for example
int (*ptr_to_array)[100] = &array;
Accessing elements
The primary facility for accessing the values of the elements of an array is the array subscript operator. To access the i-indexed element of array, the syntax would be array[i], which refers to the value stored in that array element.
Array subscript numbering begins at 0 (see Zero-based indexing). The largest allowed array subscript is therefore equal to the number of elements in the array minus 1. To illustrate this, consider an array a declared as having 10 elements; the first element would be a[0] and the last element would be a[9].
C provides no facility for automatic bounds checking for array usage. Though logically the last subscript in an array of 10 elements would be 9, subscripts 10, 11, and so forth could accidentally be specified, with undefined results.
Due to arrays and pointers being interchangeable, the addresses of each of the array elements can be expressed in equivalent pointer arithmetic. The following table illustrates both methods for the existing array:
Element | First | Second | Third | nth |
---|---|---|---|---|
Array subscript | array[0] |
array[1] |
array[2] |
array[n - 1] |
Dereferenced pointer | *array |
*(array + 1) |
*(array + 2) |
*(array + n - 1) |
Since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to *(i+a), the expression can also be written as i[a], although this form is rarely used.
Multidimensional arrays
In addition, C supports arrays of multiple dimensions, which are stored in row-major order. Technically, C multidimensional arrays are just one-dimensional arrays whose elements are arrays. The syntax for declaring multidimensional arrays is as follows:
int array2d[ROWS][COLUMNS];
where ROWS and COLUMNS are constants. This defines a two-dimensional array. Reading the subscripts from left to right, array2d is an array of length ROWS, each element of which is an array of COLUMNS integers.
To access an integer element in this multidimensional array, one would use
array2d[4][3]
Again, reading from left to right, this accesses the 5th row, and the 4th element in that row. The expression array2d[4] is an array, which we are then subscripting with [3] to access the fourth integer.
Element | First | Second row, second column | ith row, jth column |
---|---|---|---|
Array subscript | array[0][0] |
array[1][1] |
array[i - 1][j - 1] |
Dereferenced pointer | *(*(array + 0) + 0) |
*(*(array + 1) + 1) |
*(*(array + i - 1) + j - 1) |
Higher-dimensional arrays can be declared in a similar manner.