Declaration of Arrays
Like any other variables, arrays must be declared before they are used.The general form of array declaration is type variable-name [size];The type specifies the type of elements that will be contained in the array, such as int,float or char and the size indicates the maximum number of elements that can be stored inside the array.
For example: float height[50]; declares the height to be an array containing 50 real
elements. Any subscript 0 to 49 are valid. Similarly,int group[10];
declares the group to be an array to contain maximum of 10 integer constants.Remember, any reference to the array outside the declared limits would not necessarily cause an
error.Rather, it might result in unpredictable program results.The C language treats character string simply as array of characters. The size in a character string represents the maximum number of characters that the string can hold.For instance,
char name[10];
declares the name as a character array(string) variable that can hold a maximum of 10 characters.Suppose we declare the following string constant into the string variable name.
WELL DONE each character of the string is treated as an element of the array name and is stored in the memory as follows:
W
E
L
L
D
O
N
E
\0
When the compiler sees a character string, it terminates it with an additional null character.
Thus,the element name[9] holds the null character \0 at the end. When declaring character array,we must always allow one extra element space for the null terminator.
No comments:
Post a Comment