In simple English, an array is a collection.
In C also, it is a collection of similar type of data which can be either of int, float, double, char (String), etc. All the data types must be same. For example, we can't have an array in which some of the data are integer and some are float.
Why Array?
Suppose we need to store marks of 50 students in a class and calculate the average marks. So, declaring 50 separate variables will do the job but no programmer would like to do so. And there comes array in action.
How to declare an array
datatype array_name [ array_size ] ;
For example, take an array of integers 'n'.
int n[6];
n[ ]
is used to denote an array 'n'. It means that 'n' is an array.
int n[6]
means that 'n' is an array of 6 integers. Here, 6 is the size of the array i.e. there are 6 elements in the array 'n'.
We need to give the size of the array because the complier needs to allocate space in the memory which is not possible without knowing the size. Compiler determines the size required for an array with the help of the number of elements of an array and the size of the data type present in the array.
Here 'int n[6]' will allocate space to 6 integers.
We can also declare an array by another method.
int n[ ] = {2, 3, 15, 8, 48, 13};
In this case, we are declaring and assigning values to the array at the same time. Here, there is no need to specify the array size because compiler gets it from { 2,3,15,8,48,13 }
.
Index of an Array
Every element of an array has its index. We access any element of an array using its index.
Pictorial view of the above mentioned array is:
element | 2 | 3 | 15 | 8 | 48 | 13 |
index | 0 | 1 | 2 | 3 | 4 | 5 |
0, 1, 2, 3, 4 and 5 are indices. It is like they are identity of 6 different elements of an array. Index always starts from 0. So, the first element of an array has a index of 0.
We access any element of an array using its index and the syntax to do so is:
array_name[index]
For example, if the name of an array is 'n', then to access the first element (which is at 0 index), we write n[0]
.
Here,
n[0] is 2
n[1] is 3
n[2] is 15
n[3] is 8
n[4] is 48
n[5] is 13
n[0]
, n[1]
, etc. are like any other variables we were using till now i.e., we can set there value as n[0] = 5;
like we do with any other variables (x = 5;
, y = 6;
, etc.).
Assigning Values to Array
By writing int n[ ]={ 2,4,8 };
, we are declaring and assigning values to the array at the same time, thus initializing it.
But when we declare an array like int n[3];
, we need to assign values to it separately. Because 'int n[3];' will definitely allocate space of 3 integers in memory but there are no integers in that space.
To initialize it, assign a value to each of the elements of the array.
n[0] = 2;
n[1] = 4;
n[2] = 8;
It is just like we are declaring some variables and then assigning values to them.
int x,y,z;
x=2;
y=4;
z=8;
Thus, the first way of assigning values to the elements of an array is by doing so at the time of its declaration.
int n[ ]={ 2,4,8 };
And the second method is declaring the array first and then assigning values to its elements.
int n[3];
n[0] = 2;
n[1] = 4;
n[2] = 8;
You can understand this by treating n[0]
, n[1]
and n[2]
as similar to different variables you used before.
Just like variable, array can be of any other data type also.
float f[ ]= { 1.1, 1.4, 1.5};
Here, 'f' is an array of floats.
First, let's see the example to calculate the average of the marks of 3 students. Here, marks[0]
represents the marks of the first student, marks[1]
represents marks of the second and marks[2]
represents marks of the third student.
#include <stdio.h>
int main()
{
int marks[3];
float average;
printf("Enter marks of first student\n");
scanf(" %d" , &marks[0]);
printf("Enter marks of second student\n");
scanf(" %d" , &marks[1]);
printf("Enter marks of third student\n");
scanf(" %d" , &marks[2]);
average = (marks[0] + marks[1] + marks[2]) / 3.0;
printf ("Average marks : %f\n" , average);
return 0;
}
23
Enter marks of second student
25
Enter marks of third student
30
Average marks : 26.000000
Here you just saw a working example of array, we treated elements of the array in an exactly similar way as we had treated normal variables. &marks[0], &marks[1] and &marks[2] represent the addresses of marks[0], marks[1] and marks[2] respectively.
The average value should be of type 'float' because the average of integers can be float also.
Secondly, while taking out the average, the sum of the numbers should be divided by 3.0 and not 3, otherwise you will get the average value as an integer and not float.
We can also use for
loop as done in the next example.
#include <stdio.h>
int main()
{
int n[10]; /* declaring n as an array of 10 integers */
int i,j;
/* initializing elements of array n */
for (i = 0; i<10; i++)
{
printf("Enter value of n[%d]",i);
scanf("%d",&n[i]);
}
/* printing the values of elements of array */
for (j = 0; j < 10; j++)
{
printf("n[%d] = %d\n", j, n[j]);
}
return 0;
}
Enter value of n[1]34
Enter value of n[2]23
Enter value of n[3]78
Enter value of n[4]32
Enter value of n[5]21
Enter value of n[6]4
Enter value of n[7]23
Enter value of n[8]46
Enter value of n[9]24
n[0] = 12
n[1] = 34
n[2] = 23
n[3] = 78
n[4] = 32
n[5] = 21
n[6] = 4
n[7] = 23
n[8] = 46
n[9] = 24
The above code was just to make you familiar with using loops with an array because you will be doing this many times later.
The code is simple, 'i' and 'j' start from 0 because the index of an array starts from 0 and goes up to 9 (for 10 elements). So, 'i' and 'j' goes up to 9 and not 10 ( i<10 and j<10 ). So, in the code n[i]
will be n[1], n[2], ...., n[9] and things will go accordingly.
Suppose we declare and initialize an array as
int n[5] = { 12, 13, 5 };
This means that n[0]=12, n[1]=13 and n[2]=5 and rest all elements are zero i.e. n[3]=0 and n[4]=0.
Similarly,
int n[5];
n[0] = 12;
n[1] = 13;
n[2] = 5;
In the above code, n[0], n[1] and n[2] are initialized to 12, 13 and 5 respectively. Therefore, n[4] and n[5] are both 0.
Pointer to Arrays
Till now, we have seen how to declare and initialize an array. Now, we will see how we can have pointers to arrays too. But before starting, we are assuming that you have gone through Pointers from the topic Point Me. If not, then first read the topic Pointers and practice some problems from the Practice section.
As we all know that pointer is a variable whose value is the address of some other variable i.e., if a variable 'y' points to another variable 'x', it means that the value of the variable 'y' is the address of 'x'.
Similarly, if we say that a variable 'y' points to an array 'n', it would mean that the value of 'y' is the address of the first element of the array i.e. n[0]. It means that the pointer of an array is the pointer of its first element.
If 'p' is a pointer to array 'age', means that p (or age) points to age[0].
int age[50];
int *p;
p = age;
The above code assigns 'p' the address of the first element of the array 'age'.
Now, since 'p' points to the first element of array 'age', '*p' is the value of the first element of the array.
So, *p is age[0], *(p+1) is age[1], *(p+2) is age[2].
Similarly, *age is age[0] (value at age), *(age+1) is age[1] (value at age+1), *(age+2) is age[2] (value at age+2) and so on.
That's all in pointer to arrays.
Now let's see some examples.
#include <stdio.h>
int main()
{
float n[5] = { 20.4, 30.0, 5.8, 67, 15.2 }; /* declaring n as an array of 5 floats */
float *p; /* p as a pointer to float */
int i;
p = n; /* p now points to array n */
/* printing the values of elements of array */
for (i = 0; i < 5; i++ )
{
printf("*(p + %d) = %f\n", i, *(p + i) );/* *(p+i) means value at (p+0),(p+1)...*/
}
return 0;
}
*(p + 1) = 30.000000
*(p + 2) = 5.800000
*(p + 3) = 67.000000
*(p + 4) = 15.200000
As 'p' is pointing to the first element of array, so, *p
or *(p+0)
represents the value at p[0] or the value at the first element of 'p'. Similarly, *(p+1)
represents value at p[1]. And *(p+3) and *(p+4)
represents p[3] and p[4] respectively. So accordingly, things were printed.
The above example sums up the above concepts. Now, let's print the address of the array and also individual elements of the array.
#include <stdio.h>
int main()
{
int n[4] = { 20, 30, 5, 67 }; /* declaring n as an array of 4 integers */
int *p; /*a pointer*/
int i;
p = n; /*p is pointing to array n*/
/* printing the address of array */
printf("Address of array n[4] = %u\n" , p ); /*p points to array means store the address of the first element of array*/
/* printing the addresses of elements of array */
for (i = 0; i < 4; i++ )
{
printf("Address of n[%d] = %u\n" , i, &n[i] );
}
return 0;
}
Address of n[0] = 2491554384
Address of n[1] = 2491554388
Address of n[2] = 2491554392
Address of n[3] = 2491554396
As you have noticed that the address of the first element of n and p are same and this means I was not lying!
You can print other elements' addresses by using (p+1), (p+2) and (p+3) also.
Let's pass whole Array in Function
In C, we can pass an element of an array or the full array as an argument to a function.
Let's first pass a single array element to function.
#include <stdio.h>
void display(int a)
{
printf("%d\n",a);
}
int main()
{
int n[ ] = { 20, 30, 23, 4, 5, 2, 41, 8 };
display(n[2]);
return 0;
}
Passing an entire Array in a Function
We can also pass the entire array to a function by passing array name as the argument. Yes, the trick is that we will pass the address of an array, that is the address of the first element of the array. Thus by having the pointer of the first element, we can get the entire array as we have done in examples above.

Let's see an example to understand it.
#include <stdio.h>
float average(float a[])
{
int i;
float avg, sum=0;
for(i=0;i<8;++i)
{
sum+= a[i];
}
avg = sum/8;
return avg;
}
int main()
{
float b, n[ ] = { 20.6, 30.8, 5.1, 67.2, 23, 2.9, 4, 8 };
b = average(n);
printf("Average of numbers=%.2f\n",b);
return 0;
}
average(float a[])
→ It is the function that is taking an array of float. And rest of the body of the function is performing accordingly.
b = average(n)
→ One thing you should note here is that we passed 'n'. And as discussed earlier, 'n' is the pointer to the first element or pointer to the array n[]
. So, we have actually passed the pointer.
In the above example in which we calculated the average of the values of the elements of an array, we already knew the size of the array i.e. 8.
Suppose we are taking the size of the array from the user. In that case, the size of the array is not fixed. Here, we need to pass the size of array as the second argument to the function.
#include <stdio.h>
float average(float a[], int size)
{
int i;
float avg, sum=0;
for(i=0;i<size;i++)
{
sum+= a[i];
}
avg = sum/size;
return avg;
}
int main()
{
int size,j;
printf("Enter the size of array");
scanf("%d", &size);
float b, n[size];
for(j=0; j<size; j++)
{
printf("Value of n[%d] : ", j);
scanf("%f", &n[j]);
}
b = average(n, size);
printf("Average of numbers=%.2f\n",b);
return 0;
}
Value of n[0] : 12
Value of n[1] : 234
Value of n[2] : 12.43
Value of n[3] : 43.43
Average of numbers=75.46
The code is similar to the previous one except that we passed the size of array explicitly - float average(float a[], int size)
.
We can also pass an array to a function using pointers. Let's see how.
#include <stdio.h>
display(int *p)
{
int i;
for(i=0;i<8;++i)
{
printf("n[%d] = %d\n", i, *p);
p++;
}
}
int main()
{
int n[ ] = { 1, 2, 3, 4, 5, 6, 7, 8 };
display(n);
return 0;
}
n[1] = 2
n[2] = 3
n[3] = 4
n[4] = 5
n[5] = 6
n[6] = 7
n[7] = 8
In the above example, the address of the array i.e. address of n[0] is passed to the formal parameters of the function.
display(int *p)
→ This means that function 'display' is taking a pointer to an integer.
Now we passed the pointer of an integer i.e. pointer of the array n[] - 'n' as per the demand of our function 'display'.
Since 'p' is the address of the array n[] in the function 'display' i.e., address of the first element of the array (n[0]), therefore *p
represents the value of n[0]. In the for loop in function, p++
increases the value of p by 1. So, when i=0, the value of *p gets printed. Then p++ increases *p to *(p+1) and in the second loop, the value of *(p+1) i.e. n[1] also gets printed. This loop continues till i=7 when the value of *(p+7) i.e. n[7] gets printed.
2D Arrays
What if arrays are 2 dimensional?
Yes, 2-dimensional arrays also exist and are generally known as matrix. These consist of rows and columns.
Before going into its application, let's first see how to declare and initialize a 2D array.
Declaration of 2D Array
Similar to one-dimensional array, we define a 2-dimensional array as below.
int a[2][4];
Here, 'a' is a 2D array of integers which consists of 2 rows and 4 columns.
It is like
Column 0 | Column 1 | Column 2 | Column 3 | |
---|---|---|---|---|
Row 0 | a[0][0] | a[0][1] | a[0][2] | a[0][3] |
Row 1 | a[1][0] | a[1][1] | a[1][2] | a[1][3] |
Now let's see how to initialize a 2-dimensional array.
Assigning Values to a 2 D Array
Same as in one-dimensional array, we can assign values to the elements of a 2-dimensional array in 2 ways as well.
In the first method, just assign a value to the elements of the array. If no value is assigned to any element, then its value is assumed to be zero.
Suppose we declared a 2-dimensional array a[2][2]
. Now, we need to assign values to its elements.
int a[2][2];
a[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;
The second way is to declare and assign values at the same time as we did in one-dimensional array.
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
Here, value of a[0][0] is 1, a[0][1] is 2, a[0][2] is 3, a[1][0] is 4, a[1][1] is 5 and a[1][2] is 6.
We can also write the above code as:
int a[2][3] = {
{1, 2, 3},
{4, 5, 6 }
};
Let's consider different cases of assigning values to an array at the time of declaration.
int a[2][2] = { 1, 2, 3, 4 };
/* valid */
int a[ ][2] = { 1, 2, 3, 4 };
/* valid */
int a[2][ ] = { 1, 2, 3, 4 };
/* invalid */
int a[ ][ ] = { 1, 2, 3, 4 };
/* invalid */
Why use of 2 D Array
Suppose we have 3 students each studying 2 subjects (subject 1 and subject 2) and we have to display the marks in both the subjects of the 3 students. Let's input the marks from the user.
#include <stdio.h>
int main()
{
float marks[3][2];
int i,j;
for( i=0; i<3; i++)
{
/* input of marks from the user */
printf("Enter marks of student %d\n", (i+1) );
for( j=0; j<2; j++)
{
printf("Subject %d\n", (j+1) );
scanf("%f", &marks[i][j]);
}
}
/* printing the marks of students */
for( i=0; i<3; i++)
{
printf("Marks of student %d\n", (i+1) );
for( j=0; j<2; j++)
{
printf("Subject %d : %f\n", (j+1), marks[i][j] );
}
}
return 0;
}
Subject 1
78
Subject 2
67
Enter marks of student 2
Subject 1
79
Subject 2
87
Enter marks of student 3
Subject 1
90
Subject 2
89
Marks of student 1
Subject 1 : 78.000000
Subject 2 : 67.000000
Marks of student 2
Subject 1 : 79.000000
Subject 2 : 87.000000
Marks of student 3
Subject 1 : 90.000000
Subject 2 : 89.000000
This is something like
Subject 1 | Subject 2 | |
---|---|---|
Student 1 | 78 | 67 |
Student 2 | 79 | 87 |
Student 3 | 90 | 89 |