Declaring Arrays

Arrays occupy space in memory. The programmer specifies the type of each element and the number of elements required by each array so that the computer may reserve the appropriate amount of memory. To tell the computer to reserve 12 elements for integer array c, the declaration
int c[ 12 ];
is used. Memory may be reserved for several arrays with a single declaration. To reserve 100 elements for integer array b and 27 elements for integer array x, the following declaration is used:
int b[ 100 ], x[ 27 ];
Arrays may be declared to contain other data types. For example, an array of type char can be used to store a character string.

Example:  The following code uses a for repetition structure(loop) to initialize the elements of a 5-element integer array n to zeros and prints the array in tabular format.

 

#include <stdio.h> 
int main() {
	int n[ 5 ], i; 	
        for ( i = 0; i <= 4; i++ ) 
	     n[ i ] = 0;           /* initialize array */ 
	printf( "%s%13s\n", "Element", "Value" );
	for ( i = 0; i <5; i++ ) 	/* print array */ 	
             printf( "%7d%13d\n", i, n[ i ] ); 
     return 0;
} 

Output: 
Element 					Value
	0					0
	1					0	
	2					0
	3					0
	4					0

The elements of an array can also be initialized in the array declaration by following the declaration with an equal sign and a comma-separated list (enclosed in braces) of initializers.

 

#include <stdio.h> 
#define MAX 5 /* Is equal to MAX = 5*/
int main() {
	int n[ MAX] = { 32, 27, 64, 18 };  /* Initializing the array. */
	int i; 	printf( "%s%13s\n", "Element", "Value" ); 	
        for ( i = 0; i <= MAX; i++ ) 	
             printf( "%7d%13d\n", i, n[ i ] );   
        return 0;
} 

Note: If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For instance, in the following array n[4], the last element has value of 0 int n[ 5] = { 32, 27, 64, 18 };

But, if there are fewer elements than initializers, it’s an error. For instance, this will bring an error, int n[ 5] = { 32, 27, 64, 18,21,10 };

 

3. Arrays

An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.

The following figure shows an integer array called c. This array contains 12 elements. Any one of these elements may be referred to by giving the name of the array followed by the position number of the particular element in square brackets ( [ ] ). The first element in every array is the zeroth element. Thus, the first element of array c is referred to as c[ 0 ], the second element of array c is referred to as c[ 1 ], the seventh element of array c is referred to as c[ 6 ], and, in general, the ith element of array c is referred to as c[ i – 1 ]. Array names, like other variable names, can contain only letter, digit and underscore characters.

Note: Array names cannot begin with a digit character. The position number contained within square brackets is more formally called a subscript. A subscript must be an integer or an integer expression.

Continue reading