# Array in C

## Introduction

In simple words, an array is a group of variables or the linear collection of similar elements.

From “similar elements” we mean all data is of the same data type which can be either int, char, float, double, etc. If we say, some data in an array is of type ‘float’ and the rest is of type ‘char’, this is not valid.

Example, int x [5];

The above example declares an array of ‘int’ type which is a collection of 5 elements.

## Why Array in C?

The array is a method that can make our complex problems into simpler ones. Suppose a student needs to store marks of 10 subjects and calculate his overall percentage. So, declaring 10 different variables for every subject is not impossible but a bigger task, and what if the number of subjects goes to 50 or even more. Here, the array is a solution for such problems.

## Declaration of an Array in C

Syntax:

datatype array_ name [array_ size];

Example: int n[10];

In the above declaration, an array of ‘int’ type is declared.

n[ ] is used to denote an array ‘n’, which means ‘n’ is an array.

An array can be declared in many ways, but there are some ways that are not valid enough to declare an array. Some of the valid and invalid declarations are written below.

int n[10]; an array of ‘int’ type is declared of size 10 i.e., ‘n’ array is a collection of 10 elements of type ‘int’.

int n[ ]; is an invalid declaration because the size of the array is not given. It is important to declare an array with its size because the compiler needs to allocate space in the memory for the array which is not possible if the ‘[ ]’ braces are empty. ‘int n[10]’ statement is valid enough because now the compiler will get to know the size of the array with the help of the number of elements it can store and then it will allocate space to 10 elements in the memory.

int n[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; is a valid declaration. The declaration is made and values to the array are assigned simultaneously and the array is initialized as well. So, we can skip the array size because the compiler will get to know about the size from the values assigned i.e, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.

int n[10 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; is also a valid initialization.

int n[10 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; is not a valid initialization because the elements in the array are greater than the size entered and this will give an error.

 1 2 3 4 5 6 7 8 9 10

There is no space left for the rest of the elements in the array.

int n[10 ] = {1, 2, 3, 4, 5, 6, 7 } ; is a valid initialization. In such cases when the elements in the array are less than their size then there is 0 filled in remaining blocks.

 1 2 3 4 5 6 7 0 0 0

If there is no initialization with the declaration, there are garbage values i.e., 0 in all blocks.

## Index of an Array in C

Every element in an array has its particular index value.

int x[5]={10, 20, 30, 40, 50};

 Elements 10 20 30 40 50 Index 0 1 2 3 4

The array ‘x’ is represented above with its elements and the index values. 0,1,2,3,4 are the ‘indices’.

An index value of the first element is always 0 or

The index of an array starts with 0.

With the help of these indices, we can randomly access any element of the array.

Syntax: array _name [ index]

The array ‘x’ written above has 5 elements. So, we can access these elements as:

x[0] will return the value of the first element i.e., 10.

Similarly,
x[1] is 20
x[2] is 30
x[3] is 40
x[4] is 50

Moreover, x[0], x[1], are the same as the variables we were using till now. We can assign values to these variables also like x[1]=89; as we do with other variables (x=5;, y=10;, etc. )

## Assigning values to Array in C

The term ‘initialization’ discussed above will become clear with this topic.

Values can be assigned to an array in different ways. The first way is to assign the values to the elements of the array during the declaration.

int n[ ] = {1, 2, 3, 4, 5 } ; by writing this, we are declaring and assigning values to the array at the same time. Thus, we have initialized an array.

The second way is to declare the array first and then assign the values separately.

int n[3]; This statement declares an array but the values are not assigned yet. ‘int n[3]’ will allocate space of 3 integers in the memory but until and unless there is no assignment of values, those spaces remain empty. To initialize it, we have to assign a value to each of the elements of the array-like,

n[0]=2;

n[1]=9;

n[2]=6;

This is the same as we declare variables and then assign values to them.

int x, y;

x=89;

y=10;

In short, the two methods of assigning values to an array are:

Method:1 Declaration and assignment are done at the same time.

int n[ ]= {1,2,3,4,5 };

Method:2 Firstly, declare the array with its size. Assign the values to the elements of the array later.

int n[ 3];

n[0]=5;

n[1]=8;

n[2]=4;

The array can be of any data type int, char, float, etc. See the examples of arrays of different data types.

float f[ ]={ 1.1, 2.2, 3.3};

char c[ ]={‘a’, ‘b’, ‘c’};

Let’s make a program to calculate the average marks and the overall percentage of a student in his 5 subjects. Each subject is of 100 marks and the subjects are Physics, Chemistry, Mathematics, English, and Computer.

PROGRAM

```#include<stdio.h>

int main()

{

int M[5];

float A;

printf("Enter marks in Physics:");

scanf("%d",&M[0]);

printf("Enter marks in Chemistry:");

scanf("%d",&M[1]);

printf("Enter marks in Mathematics:");

scanf("%d",&M[2]);

printf("Enter marks in English:");

scanf("%d",&M[3]);

printf("Enter marks in Computer:");

scanf("%d",&M[4]);

int sum=( M[0]+M[1]+M[2]+M[3]+M[4]);

A=sum/5.0;

printf("Average marks:%.2f",A);

float percentage=(sum/500.0)*100;

printf("\nPercentage=%.2f",percentage);

return 0;

}```

OUTPUT:

An example related to our practical life makes the concept more clear and understanding even better.

int M[5]; here, we just declare an array of size 5 without initializing it because we want the user to enter his/her own data.

After that, while taking input from the user, we treated elements of an array in an exactly similar way as we had treated normal variables. &marks[0], &marks[1] represent the addresses of marks[0], marks[1] respectively.

The rest of the code is having the operation that is needed to calculate the average marks and percentage.

A=sum/5.0; ‘A’ is declared as float type variable because the average of integers values can be float also. That’s why the division of ‘sum’ is made with 5.0 so that it returns a float value which will then be assigned to ‘A’. But if the ‘sum’ is divided by 5 then it will give an integer value as result.

## Use of for loop in Arrays in C

Loops with arrays are the easiest way to tackle hard problems.

PROGRAM

```#include<stdio.h>

int main( )

{

int i,j;

int A[5];

for(i=0; i<5;i++)

{

printf("Enter the value of n[%d]",i);

scanf("%d",&A[i]);

}

for(j=0;j<5;j++)

{

printf("A[%d]=%d\n",j,A[j]);

}

return 0;

}```

OUTPUT:

The above code is the simplest approach to make you familiar with loops and arrays. We declared two variables ‘i’, ‘j’ as iterators and an array ‘A’ of size 5. The code is simple, firstly we have to input our data, and then the entered data will be shown as output.

The first ‘for’ loop having ‘i’ iterator is for taking inputs from the user.

for(i=0; i<5;i++) ‘i’ is initialized with 0 since starting index of array is 0 and goes up to 4.

Same is with for(j=0;j<5;j++) statement ‘j’ starts from 0 and ends at 4. This ‘for’ loop will display the data entered by the user in a sequence.

Nothing is complex as we can observe in the output section. As already said, treat elements of the array as normal variables and things will go accordingly.

Note: The memory allocation in an array is in a continuous manner. For example, if the address of the first element of an array of index 0 is 22334456 then the address value of the second element with index 1 will be 22334460 (22334456+4; since 4 is the size of an integer) and third will be 22334464 and so on. It is important to note that all elements of an array are stored together in a serial order.

## Pointer of an Array in C

Every concept in a programming language is developed to make the other tools easier. The use of the array in the pointers is a very important topic. So, we all are done with the declaration and initialization of an array. Now we will see how we can use arrays in pointers. But before proceeding, we hope that you have gone through the topic pointers and practice enough problems related to it. If not, then first clear out the basics of pointers to get a better understanding of this interesting concept.

Since we all know ‘Pointer is a variable that points to another variable. It stores the address of the value of the variable to which it points. Say ‘A’ points to ‘B’ means both ‘A’ and ‘B’ are variables but ‘A’ is a pointer. The value of ‘A’ is the address value of ‘B’.

Suppose we have an array named ‘n’. ‘p’ is a variable that points to ‘n’ which means ‘p’ is a pointer to ‘n’. Now the array consists of a number of elements so the question arises as to which element of the array, pointer ‘p’ points.

Pointer ‘p’ contains the address value of the first element i.e. n[0] of the array.

The pointer of an array is the pointer to its first element or the name of the array is the pointer to the first element of the array.

```int student [5];

int *p;

p=&student[0];```

Now let us understand the code written above.

‘student’ is an array and ‘p’ is a pointer to the ‘student’ array then ‘p’ points to student[0]. That’s why the statement p=&student[0]; assigns the address value of the first element of the array to ‘p’.

int student [5];

int *p;

p=student;

The above-written code is also valid. The statement p=student; works the same as the previous one. The name of the array itself is the pointer to the first element of the array.

p=student; this statement also assigns ‘p’ the address of the first element of the array ‘student’.

Since ‘p’ points to the first element of the array then the value of ‘*p’ will be equal to the first element of the array i.e. *p= student[0]

What about the rest of the elements of the array then?

If ‘*p’ refers to the first element of the array then *(p+1) refers to the second element of the array, *(p+2) refers to the third element of the array, and so on.

So, we are done with the theory part of pointers related to the array. Now, look at the programs which will give you a better understanding.

PROGRAM

```#include<stdio.h>

int main()

{

int A[5]={12,24,36,48,60};

int *p;

int i;

p=A;

for(i=0;i<5;i++)

{

}

return 0;

}```

OUTPUT:

So first of all we proved that the name of the array is the pointer to the first element of the array. As the statement p=A; gives the output which is equal to the address value of the first element.

After using the ‘for’ loop we print the address value of every element that clearly shows that all values are in increasing order with a difference 4.

PROGRAM

```#include<stdio.h>

int main()

{

int A[5]={12,24,36,48,60};

int *p;

int i;

p=A;

for(i=0;i<5;i++)

{

printf("Value of *(p+%d)=%d\n",i,*(p+i));

}

return 0;

}```

OUTPUT:

As ‘p’ is pointing to the first element of the array, thus *p or *(p+0) contains the value of A[0]. Similarly, *(p+1) is equal to A[1] and so on as shown in the output window.

## Author

• A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.