An array is a kind of variable that refers to a collection of data items which all have same name and same data type. There are some applications that require processing of multiple data items that have common characteristics. In situations like this it is convenient to place the data items into an array, where they all share the same name. The individual data items can be integers, characters, and so on. They must all be of same type and same storage class.
Defining an Array
Arrays are defined in the same manner as ordinary variable, except that the Iarray name must be accompanied by the number of elements (i.e. the size of Ithe array). In case of one-dimensional array the size is specified by a positive Iinteger enclosed in square brackets. I
General definition of an array
Storage- class data-type array [expression]
Example
int x[100]; /* 100 element integer array*/ char name[30]; /* 30 element character array*/
About Subscript and Dimensionality
Each array element of an array is referred to specifying the array name followed by one or more subscripts, each enclosed in square brackets. The subscript should be a non-negative integer.
Example:
For a n element array x (i.e. x[n]) the array elements arex[0],x[1],x[2]......... x[n-1]
The number of subscripts determine the dimensionality of an array. For example x[i] refers to an element of an one dimensional array. Similarly x[i refers to an element of an two dimensional array. A two dimensional array can be visualized as a table where x[i](j] refers to the (j+1) th element of (i+1)th row.
Array Initialization
Automatic arrays unlike automatic variables can not be initialized although I external and static array definitions can include the assignment of initial values depending on the requirement. The individual values must appear in the same order in which they will be assigned to individual array elements, enclosed in braces and separated by comma.
The general form is
Storage class data-type array [expression]={value 1,value 2,value3................. value n}
Where value 1 refers to the value of the first element in the array, value 2 refers to the value of the second array element and so on.
Array size need not be specified explicitly when initial values are included as a part of an array definition .for numerical array .the array size will automatically set equal to the number of initial values included within the definition.
Example:
static int x[7]={1,2,3,4,5}; static int y()={1,2,3,4,5};
The individual array elements will be assigned as follows:
For the first array definition
x[0]=1 x[1]=2 x[2]=3 x[3]=4 x[4]=5 x[5]=0 x[6]=0
For the second array definition y[0]=1 y[1]=2 y[2]=3 y[3]=4 y[4]=5
When a string constant is assigned to an external or static character array as part of array definition, the array size specification is usually omitted The proper array size is assigned automatically. This will include a provision for the null character \0, which is automatically added at the end of every string.
Example
static char x[8]="COMPUTER"; static char yQ="COMPUTER";
The individual array elements of the array x and y array will be
xlOJ^C' x[1]='0' x[2]='M' x[3]='P' xl4}='U' x[5]-T' x[6]='E' y\l]='R
y[0]='C- y[1]='0' y[2]='M' y[3]='P' y[4]=V y[5]=T y[6]='E' y[7]='R' y[8]='\0'
Thus the first form of array definition is not correct, as the null character is not included in the array. That array definition could have been written as
x[9]="COMPUTER";
Processing an Array
C does not permit single operations involving entire arrays. For example if x and y are array of same data type .dimensionality and size , assignment operation, comparison operation and so on must be carried out on an element by element basis. This is accomplished using a loop .where each pass through
the loop is used to process each array element of the array. So the number of pass through the loop will be equal to the number of array element to be processed.
Example #include
Main ()
{
intl, x[10], sum=0;
for (l=0;K 10;++l)
{
printf ("I %d x=",l+1);
scanf ("%d" , &x[l]); sum=sum+x[l];
}
printf ("\nSum of numbers=%d", sum);
}
Passing an Array to a Function
To pass an array to a function, the array name must appear by itself, without brackets or subscripts as an actual argument within the function call. The corresponding formal argument is written in the same manner, though it must be declared as an array within the formal argument declaration. When declaring one-dimensional array as a formal argument, the array name is written with an empty pair of square brackets. The size of the array is not declared within the formal array declaration. The example below gives the outline of a program that illustrates passing of an array from the main portion of the program to a function
Example:
#include
main()
{
int n, sum, numbers[50],i;
int total(); /* function declaration*/
n=50;
for(i=0;i<=n; i++) numbers[i]=i; sum=total(n, numbers); printf("sum=%d", sum ); } int total(m. y) /* function definition*/ int m; /* formal argument declaration*/ int y(); /* formal argument (array) declaration*/ { int j; int temp=0; for(j=0;j<=m;j++) { temp= temp+yO]; } return(temp); } Writing function declarations that include argument type specification requires special attention. If any of the arguments are array, an empty pair of square bracket must follow the data type of array. In case of function prototypes an empty pair of square bracket must follow the data type of each array argument. Similarly if the first line of function definition includes the formal argument declaration, an empty pair of square bracket must follow each array name appearing as a formal argument. The example given now is similar to the last example except that now the argument specifications are included within the function declaration and the formal argument declarations are combined with the first line of the function definition.
Example:
#include
main ()
{
int n, sum, numbers [50],i;
int total(int, int []); /* function declaration*/
n=50;
for (i=0;i<=n; i++) numbers[i] =i; sum=total (n, numbers); printf ("sum=%d", sum); } int total (int m, int y[]) /* function definition*/ { intj; int temp=0; for(j=0;j<=m; j++) { temp= temp+y[j]; } retum (temp); }
Array and Passing Argument by Reference
When an array element is passed to a function, the values of the array elements are not passed to the function. Rather the array name is interpreted as the address of the first array element. This address is assigned to the corresponding formal argument when the function is called. So the formal argument becomes the pointer (We will take up pointers in detail in a later chapter. For now just remember that a pointer is a variable that stores the address of another variable) to the first element of the array. The argument passed in this manner is said to be passed by reference rather than by value. If an array element is altered within the function the change will be recognized in the calling portion of the program (actually throughout the scope of array definition).