You are on page 1of 3

1.10.3 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 variablelength 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, which must be at least one byte longer than the string. A wide string consists of characters of type wchar_t and is terminated by the wide null character, L'\0'. The length of a wide string is the number of wchar_t characters in the string, excluding the wide string terminator. For example: wchar_t wstr[20] = L"Mister Fang"; // // length: 11 wide characters

A multi-dimensional array in C is an array whose elements are themselves arrays. For example: short point[50][20][10]; The three-dimensional array point consists of 50 elements that are two-dimensional arrays. The declaration above defines a total of 50*20*10 = 10,000 elements of type short, each of which is uniquely identified by three indices:

point[0][0][9] = 7;

// Assign the value 7 to the "point" // with the "coordinates" (0,0,9).

Two-dimensional arrays, also called matrices, are the most common multi-dimensional arrays. The elements of a matrix can be thought of as being arranged in rows (first index) and columns (second index). Arrays in C are closely related to pointers: in almost all expressions, the name of an array is converted to a pointer to the first element of the array. The sizeof operator is an exception, however: if its operand is an array, it yields the number of bytes occupied, not by a pointer, but by the array itself. After the declaration: char msg[] = "Hello, world!"; the array name msg points to the character 'H'. In other words, msg is equivalent to &msg[0]. Thus in a statement such as: puts( msg ); // Print string to display only the address of the beginning of the string is passed to the function puts(). Internally, the function processes the characters in the string until it encounters the terminator character '\0'. An array is initialized by an initialization list containing a constant initial value for each of the individual array elements: double x[3] = { 0.0, 0.5, 1.0 }; After this definition, x[0] has the value 0.0, x[1] the value 0.5, and x[2] the value 1.0. If the length of the array is greater than the number of values in the list, then all remaining array elements are initialized with 0. If the initialization list is longer than the array, the redundant values are ignored. The length of the array need not be explicitly specified, however: double x[] = { 0.0, 0.5, 1.0 }; In this definition, the length of the array is determined by the number of values in the initialization list. A char array can be initialized by a string literal: char str[] = "abc"; This definition allocates and initializes an array of four bytes, and is equivalent to: char str[] = { 'a', 'b', 'c', '\0' } ;

In the initialization of a multi-dimensional array , the magnitude of all dimensions except the first must be specified. In the case of a two-dimensional array, for example, the number of rows can be omitted. For example: char error_msg[][40] = { "Error opening file!", "Error reading file!", "Error writing to file!"};

The array error_msg consists of three rows, each of which contains a string. 1.10.4 Pointers

You might also like