The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# C Tutorial

## Arrays in C

 Module Index ► Introduction to Arrays ► Passing array to function ► Pointer to an array ► Two Dimensional ( 2D ) Arrays ► Passing 2D Array to Function

Introduction to Arrays
Suppose you want to store the marks of 100 students of a class and then find the average. Would you declare 100 variables for that ? Definitely not. So, here comes a data structure called array which provides an utility to store a collection of elements of same data type in contiguous memory location.
Consider the declaration below :

 int marks [5];

In the above statement, we have declared an array named marks which can store 5 values of type int. Each of the values can be accessed using an index in the array.
Suppose there are 5 values stored in the array marks as show below :

 75 62 93 49 88

Value stored at index 0 of array marks is 75 i.e marks [ 0 ] = 75. Remember array index starts from 0.
Similarly, marks [ 2 ] = 93. The array marks can be initialized with 5 values in a single statement :

 int marks [ ] = { 75, 62, 93, 49, 88 };

We see that size of the array is not specified in the above statement. Since, we have initialized the array with 5 values, an array of size 5 is automatically created. We can explicitly specify the size of the array. In that case, if we specify the size as 5, we can initialize with 5 elements only. Consider the following program which takes marks of 5 students in an array and then find the sum and average of all marks :

Run this program in your system to take input at run-time

Passing array to function
Arrays are by default passed by reference. We can pass an array to a function either by using the array name
( arr ) or the address of the first element of the array ( &arr [ 0 ] ) . Similarly, we can receive the passed array either using an array ( int arr[ ] ) [ see the insertElement( ) , deleteElement( ) , and searchElement( ) functions below ] or a pointer to an array ( int *arr ) [ see the displayArray( ) function below ] . Consider the following program in which we implement four functions ( insert, delete, search and display ) :

Sorting is another popular operation on array elements. Please refer to the sorting algorithm section to understand how different sort techniques work.

Pointer to an array
We have seen that a pointer variable stores the address of another variable. A pointer variable can point to an array as well. Consider the following statements :

 int marks [ ] = { 75, 62, 93, 49, 88 }; int *ptr = marks; // ptr points to first index of the array int *ptr = &marks[0]; // This statement is same as the above one

We declared a pointer variable ptr which points to the first element of the array. Since array elements are stored in contiguous memory locations, all the elements can be accessed using ptr. Moreover, a pointer variable can point to any index of the array. Following program demonstrates the use of pointer to an array :

Two Dimensional ( 2D ) Arrays
So, far we have seen one dimensional arrays. Now we will see two dimensional array also known as matrix, which is another useful data structure. Consider the declaration below :

 int mat [ 3 ][ 4 ];

We have declared a two dimensional array or matrix with 3 rows and 4 columns. We can store 3 x 4 = 12 elements in this matrix. Each element is stored in a cell which can be accessed using the combination of
row index and column index. Initialiation of a 2D array is shown below :

InitializationIndexing
``````int mat[3][4] = { { 17, 23, 15, 19 },
{ 44, 29, 52, 76 },
{ 21, 63, 35, 57 } };``````

 17 [ 0, 0 ] 23 [ 0, 1 ] 15 [ 0, 2 ] 19 [ 0, 3 ] 44 [ 1, 0 ] 29 [ 1, 1 ] 52 [ 1, 2 ] 76 [ 1, 3 ] 21 [ 2, 0 ] 63 [ 2, 1 ] 35 [ 2, 2 ] 57 [ 2, 3 ]

We can see how the above initialization statement in the left builds a matrix shown in the right. Every element is stored in a cell which has a index shown as subscript of each element. For e.g, 52 is stored in a cell with index
[ 1, 2 ] where 1 denotes the row number ( index ) and 2 denotes the column number ( index ). Please note that both row and column index starts with 0.
Following program inputs a matrix from user and displays it :

Run this program in your system to take input at run-time

Passing 2D Array to Function
2D Arrays (matrices) are passed similar to 1D arrays but the no. of columns must be specified in the argument of the function to which a matrix has been passed. In the following program we implement four functions which takes a matrix as an argument and perform the following functions :
1) Compute sum of left diagonal elements
2) Compute sum of right diagonal elements
3) Print the lower diagonal matrix
4) Print the upper diagonal matrix