Arrays
Array is the simplest and most commonly used structured type. A variable of array type is actually an array of objects of the same type. These objects represent elements of an array and are identified by their position in array. An array consists of a contiguous region of storage exactly large enough to hold all of its elements.
Array Declaration
Array declaration is similar to variable declaration, with the brackets added after identifer:
type array_name[constant-expression]
This declares an array named as array_name
and composed of elements of type
.
The type
can be any scalar type (except void
), user-defined type, pointer, enumeration, or another array. Result of constant-expression
within the brackets determines a number of elements in array. If an expression is given in an array declarator, it must evaluate to a positive constant integer. The value is a number of elements in an array.
Each of the elements of an array is indexed from 0 to the number of elements minus one. If a number of elements is n
, elements of array can be approached as variables array_name[0] .. array_name[n-1]
of type
.
Here are a few examples of array declaration:
#define MAX = 50 int vector_one[10]; /* declares an array of 10 integers */ float vector_two[MAX]; /* declares an array of 50 floats */ float vector_three[MAX - 20]; /* declares an array of 30 floats */
Array Initialization
An array can be initialized in declaration by assigning it a comma-delimited sequence of values within braces. When initializing an array in declaration, you can omit the number of elements – it will be automatically determined according to the number of elements assigned. For example:
/* Declare an array which holds number of days in each month: */ int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; /* This declaration is identical to the previous one */ int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
If you specify both the length and starting values, the number of starting values must not exceed the specified length. The opposite is possible, in this case the trailing “excess” elements will be assigned to some encountered runtime values from memory.
In case of array of char
, you can use a shorter string literal notation. For example:
/* The two declarations are identical: */ const char msg1[] = {'T', 'e', 's', 't', '\0'}; const char msg2[] = "Test";
For more information on string literals, refer to String Constants.
Arrays in Expressions
When the name of an array comes up in expression evaluation (except with operators &
and sizeof
), it is implicitly converted to the pointer pointing to array’s first element. See Arrays and Pointers for more information.
Multi-dimensional Arrays
An array is one-dimensional if it is of scalar type. One-dimensional arrays are sometimes referred to as vectors.
Multidimensional arrays are constructed by declaring arrays of array type. These arrays are stored in memory in such way that the right most subscript changes fastest, i.e. arrays are stored “in rows”. Here is a sample of 2-dimensional array:
float m[50][20]; /* 2-dimensional array of size 50x20 */
A variable m
is an array of 50 elements, which in turn are arrays of 20 floats each.
Thus, we have a matrix of 50x20 elements: the first element is m[0][0]
, the last one is m[49][19]
.
The first element of the 5th row would be m[4][0]
.
If you don't initialize the array in the declaration, you can omit the first dimension of multi-dimensional array. In that case, array is located elsewhere, e.g. in another file. This is a commonly used technique when passing arrays as function parameters:
int a[3][2][4]; /* 3-dimensional array of size 3x2x4 */ void func(int n[][2][4]) { /* we can omit first dimension */ ... n[2][1][3]++; /* increment the last element*/ } void main() { ... func(a); }
You can initialize a multi-dimensional array with an appropriate set of values within braces. For example:
int a[3][2] = {{1,2}, {2,6}, {3,7}};
What do you think about this topic ? Send us feedback!