Arrays
Arrays are used to manage large numbers of objects of the same type. Arrays in C can have elements of any type except a function type. The definition of an array specifies the array name, the type, and, optionally, the number of array elements. For example:
char line[81];
The array line consists of 81 elements with the
type char. The variable line
itself has the derived type “array of
char" (or
"char array”).
In a statically defined array, the number of array elements (i. e., the length of the array) must be a constant expression. In ANSI C99, any integer expression with a positive value can be used to specify the length of a non-static array with block scope. This is also referred to as a variable-length array .
An array always occupies a continuous location in memory. The size of an array is thus the number of elements times the size of the element type:
sizeof( line ) == 81 * sizeof( char ) == 81 bytes
The individual array elements can be accessed using an index. In C,
the first element of an array has the index 0. Thus the 81 elements
of the array line are line[0],
line[1], ... ,
line[80].
Any integer expression can be used as an index. It is up to the programmer to ensure that the value of the index lies within the valid range for the given array.
A
string
is a sequence of consecutive elements of type char
that ends with the null character, '\0'. The
length of the string is the number of characters excluding the string
terminator '\0'. A string is stored in a
char array, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access