What is Array in C?


Array in C

Before knowing about the initialization and declaration of the array in C, it is important to know and understand why we need an array in C? Suppose we have to save the details of one, two, or ten students in a college, then it is easy to declare 10 variables. But if you have to store more than 100 students details, then it is tough to think and write about so many variables. But with the help of arrays, we can store the information of many people by declaring only one variable. 


Data or information in the array is stored in the contiguous memory location. In the array, we can store the same data type information. Now, let's learn how to create an array.


See Also: Strings in C

Declaration of an Array in C


Whenever you create an array, you define the name of the array and how many values it is going to store in it. As if you want to store 6 numbers, you can create an array for that. The general form of creating an array in C is given below.

Data type     Name of array-variable[Size];

By size, you have to tell how many values you want to store.

For example

int arr[6];

Where 'arr' is the name of the array and 6 shows the size of an array i.e., 6 data values you can store in an array 'arr.' 






Initialization of an array in C


We have learned how to declare an array. Now we will learn how to store the values in an array. I want to tell you that whenever we declare an array, the location is allocated in memory according to its size and that location is allocated with the same number of index numbers. An index number is a unique name for every location. You should always remember one thing is that the index of the array always starts with zero.

arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
arr[5]

In the declaration of an array, you can insert value in this way,


See Also: Structure in C


arr[0] =10;
arr[1] = 67;
arr[2] = 98;
arr[3] = 68;
arr[4] = 34;
arr[5] = 54;

or you can insert value in an array like this,

int arr[6] = {10,67,98,68,34,54};

And if you want to give values during the run time of program then you can put values in this way,

for (i=0; i <= 6; i++)
{
printf ("Enter values in an array");
scanf ("%d", &arr[i]);
}

Accessing array elements 


If we want to access data from the array then 

printf ("Data of array = %d\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
  
The values of the array will be printed on the statements given above, and if you want to perform an operation, you can access their values by writing the direct array name arr[0], arr[1], etc.


See Also: Union in C


And if you want to access the values of the array then you can use the loop,

for (i = 0; i < 6; i++)
{
printf ("%d\n", arr[i]);
}


C program to demonstrate the initialization and accessing of array elements.


#include <stdio.h>
#include <conio.h>
int main( )
{
    //Declaration of an array
    int arr[6]; int i;

    //Initialization of an array
    printf ("Enter array element\n");
    for (i=0; i<6; i++)
    {
        scanf("%d",&arr[i]);
    }

    //Print array element
    for (i=0; i<6; i++)
    {
        printf ("%d\n", arr[i]);
    }
    return 0;
}


Output

Array in C

How to calculate the size of An array in C


Array in C
Size of an array in C

#include <stdio.h>
int main( )
{
    int arr[5];
    int size, size_of_array, n;

// Determine the size of an array
    size = sizeof (arr);
    printf ("size=%d\n", size);

// Determine the size of an element in an array
     n = sizeof (arr[0]);
     printf ("n=%d\n", n);

// Determine number of elements in an array
    size_of_array = sizeof (arr)/ sizeof (arr[0]);
    printf ("size_of_array=%d\n", size_of_array);

    return 0;
}


Output
size=20
n=4
size_of_array=5


2-dimensional Array  in C


In the 2-dimensional arrays, the data is stored in the form of the table. For example, we want to store the roll number of students, their names, and their marks, then we will declare them using a 2-dimensional array.


Array in C
2-dimensional array

Declaration of a 2-dimensional array in C


A 2-dimensional array is declared in the form of row and column,

 Data type  name of an array [row][column];


The roll number of students, their name and their marks will be written using this 2-dimensional array.

int arr[4][3];

4 rows and 3 columns (the first column is of the roll number of students, second is their name, and the third column is of marks as shown in the above figure).

Initialization of a 2-dimensional array in C


int arr[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12};

OR

int arr[4][3] ={{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};


In this initialization, the set of each inner braces represents a row. There is a total of 4 rows, so there are 4 sets of internal braces.

Accessing 2-dimensional array in C


To access any single element in the 2-dimensional array, write it like this,

#include <stdio.h>
int main ( )
{
    //Declaration and initialization of Two-Dimensional Array
    int arr[4][3] = {101,102,103,104,105,106,107,18,109,10,11,12};

    //Access first element of third row
    printf ("%d\n", arr[3][0]);
return 0;
}

Output
10


To access all the elements of the two-dimensional array, we have to use two loops, one for loop for rows and another for loop for the column. 


#include <stdio.h>
int main ( )
{
    int arr[4][3]= {101,102,103,104,105,106,107,18,109,10,11,12};
    int i,j;

    for (i=0; i<4; i++)     // This loop is for row
    {
        for (j=0;j<3;j++)   // This loop is for column
        {
                printf ("%d\n", arr[i][j]);

        }
    }
return 0;
}

Output

101
102
103
104
105
1006
107
108
109
10
11
12

Pass an array to the function


A program below has been written in which ae are adding values of two arrays, and it has also been shown the way how to pass an array to the function.

See Also: Use of Array of Function Pointer in C


#include <stdio.h>

// Declaration of function to add array
int add_array(int arr[ ], int brr[ ], int crr[ ]);

int main ( )
{
    //Declaration of the integer i.
    int i;

    //Declaration and initialization of array arr and brr

    int arr[5] = {1,2,3,4,5};
    int brr[5] = {1,2,3,4,5};
    int crr[5];

    add_array(arr,brr,crr); //Calling Function add_array

    for (i=0;i<5;i++)
    {
        printf ("%d\n", crr[i]);
    }
    return 0;
}


int add_array(int arr[5], int brr[5], int crr[5])
{
    int j;
    for (j=0;j<5;j++)
    {
        crr[j]=arr[j]+brr[j];
    }
    return crr;
}


Output
2
4
6
8
10



C program to demonstrate the passing 2-dimensional array to the function  


#include <stdio.h>
#define M 2
#define N 2
int add_2d_array(int a[][N], int b[][N], int c[][N]);
int main ( )
{
    int a[M][N]={1,2,3,4};
    int b[M][N]={1,4,6,7};
    int c[M][N]; int i,j;

    add_2d_array(a,b,c);

    for (i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            printf ("%d\n", c[i][j]);
        }
    }
    return 0;
}

int add_2d_array(int a[][N], int b[][N], int c[][N])
{
    int i,j;
    for (i=0;i<M;i++)
    {
        for (j=0;j<N;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }
    return c;
}


Output
2
6
9
11


Pointer to array


The array is also called a constant pointer. This pointer points to the first element of the array. Whenever you create an array, this pointer automatically created. So, if you assign an array name to a pointer variable, then there is no need to use an address of (&) operator before the name of the array. Because as I mentioned earlier, an array name is already a constant pointer.

#include <stdio.h>
int main ( )
{
    // Declaration and initialization of array
    int array[5] = {11,22,33,44,55};
    int *qtr; int i;
    qtr = array;

    // Print 0th element of an array
    printf ("%d\n", *qtr);

 // Print all elements of an array
    for (i=0; i<5; i++)
    {
        printf ("%d\n", *(qtr+i));
    }

    return 0;
}



Output
11
11
22
33
44
55


Pointer to a 2-dimensional array in C


#include <stdio.h>
int main()
{
    // Declaration and initialization of 2-d array in C
    int array[3][2] = {
        {11,22},
        {44,55},
        {77,88}
        };
    int *qtr; int i,j;
    qtr = array;

 // Print all elements of an array
    for (i=0; i<3; i++)
    {
        for (j=0;j<2;j++)
        {
        printf ("%d", *(*(array + i) + j));
        printf ("\n");
        }
    }

    return 0;
}


Output

11
22
44
55
77
88



What is Array in C? What is Array in C? Reviewed by Glory on 13:19 Rating: 5
Powered by Blogger.