What are Arrays in C Programming?

Hey Guys, Welcome, in this module we are going to discuss What are arrays in C Programming. The prerequisite of this module is that you should know all the concepts which we have covered before, especially variable and data types.

So, let’s start our journey towards our module.

What are arrays in C Programming?

Arrays in C are defined as the collection of items of a particular data type, and these data types are stored at contiguous memory locations. You can declare the array of any data type as per your requirements like int, char, float, double, etc.

What are arrays in C Programming

It is the type of data structure in which you can store sequential elements of fixed size and of the same type. Suppose you want to store multiple values in a single variable, then for that purpose Arrays are used.

How to declare an array in C Programming?

To declare an Array, define the array type, specify the array name followed by square brackets, and specify the number of elements you want to store in the array.

The syntax for declaring the array in C is:

Data_type array_name[size of the array];

For example:
int marks[20];

In this example, we have declared an array named marks of size 20 of type int, which means we can store a total of 20 marks of the student in the array named array of type integers.

Now since we have declared an array, the point comes to assign a value to it. To assign a value to it we can achieve this by following.

int num[ 5 ] = {100, 30, 50, 20, 120};

In this, we have declared an array named num of size 5 of integer type, and we know that we can store a total of 5 integers in this array, so we can assign the 5 values in this manner.

How to access the elements of Arrays?

Now, we have declared the array, and also have assigned the values to it, now the matter comes is that how can we access the elements of that array. We can achieve this by referring to the index numbers.

The index number is the location or position in which the array elements are being stored. Index number always starts from 0, i.e., the first element of the array is stored at index 0, the second at index 1, and likewise it goes on.

Suppose you have initialized the array as:

int x[6] = { 2, 4, 6, 8, 10, 12 }

In this example, x is the array name, that can store a total of 6 elements of integer type. Now we have to access the 1st element, we can achieve this by x[0];
The diagrammatic view of this is:
How to access the elements of Arrays

In this diagram, we have shown the scenario that how arrays are stored in their indexes and how you can access the elements by using the index numbers.

Suppose, the size of the array is n, and you have to access the last element, then the index of the last element will be n – 1.

What happens when arrays are left with empty members?

In C, if we have declared an array of size n, then that means we can store a total of n elements in that array, now suppose if you store elements less than n, then in that case what will happen, in that case, the array will be left empty, and either stores 0 in that place or some garbage elements.

For example,

int x[6] = {13, 20, 80};

Here, the array x has been declared of the size of 6, and then we have initialized it with only 3 elements. In such cases, the compiler assigns random values or it may be some garbage values to the remaining places. Oftentimes, this random value is simply 0.

Let’s analyze this with diagrammatic visualizations:

What happens when arrays are left with empty members

In this diagram we can clearly see, that array has stored 0 in the remaining places because it has no elements to store in that place.

How to change the values of the array’s elements?

In this section, we will see that how can we change the array elements once declared and initialized. So, we can do this by referring to the index number, for example.

int marks[ 5 ] = {50, 70, 60, 40, 90};

Now, in this array we have to change the 3rd element to 35, we can do this simply by referring to the index of 3rd elements, i.e., the index of 3rd elements is 2, i.e.,

marks[ 2 ] = 35;

This way we can change any elements as per requirements.
Let’s see the sample example representing the use of arrays.

#include <stdio.h>
int main()
{   int size;      // variables declared for taking the size of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);       // size input
    int arr[size];      // declaring the array
    printf("Enter the elements of the array: ");
    for(int i = 0; i<size; i++)  // loop declared for accessing the elements of arrays.
    {   
        scanf("%d", &arr[i]);  // elements of array input
    }
    printf("The elements of the array are: ");
    for(int i = 0; i<size; i++)  // loop declared for accessing the elements of arrays. 
    {
       printf(" %d", arr[i]);  // printing the elements of arrays
    }
    return 0;
}

The output of the above program is:

Enter the size of the array: 4
Enter the elements of the array: 34 45 67 18
The elements of the array are: 34 45 67 18

In this program, we are simply asking the user to enter the size of the array and then the elements of the array and then printing that element as output.

Write a program to reverse an array element?

#include <stdio.h>
int main()
{   int size;      // variables declared for taking the size of the array
    printf("Enter the size of the array: ");
    scanf("%d", &size);    // size input
    int arr[size];      // declaring the array
    printf("Enter the elements of the array: ");
    for(int i = 0; i<size; i++)
    {   
        scanf("%d", &arr[i]);    // elements of array input
    }
    printf("The reverse elements of the array are: ");
    for(int i = (size – 1); i>0-1; i--)
    {
       printf(" %d", arr[i]);   // printing the reverse elements of arrays
    }
    return 0;
}

The output of the above program is:

Enter the size of the array: 4
Enter the elements of the array: 34 45 67 18
The elements of the array are: 18 67 45 34

In this program, we are reversing the array elements and then printing the same as output.

What are Multi-dimensional arrays in C?

If you want to create a matrix with code, then you have to go for the multi-dimensional array, in this case, 2-D array, i.e., it is used to create arrays of the array. You can create the following by:

int x[2][3];

In this, I have created a 2-D array named x of int data type, and it can hold a maximum of 6 elements, i.e., 2*3, here 2 depicts the number of rows and 3 depicting the number of columns.

Let’s visualize it diagrammatically.

Write a program to reverse an array element

From this diagram, we can visualize how a 2-d array looks like.
To initialize a 2-D array, we can do this by following.
int test1[2][3] = { 1, 2, 3, 4, 5, 6 };              OR
int test1[2][3] = { {1, 2, 3}, {4, 5, 6} };

 2-D array

This is how elements are stored in a 2-D array.
In the same manner, we can declare a 3-D array, for example:

int x[2][3][4];

Here, x is the 3-dimensional array, that can store a total of 24 elements i.e., 2*3*4.

What are the advantages of the array?

We have covered a lot about arrays, let’s see some of the advantages of it.

  1. We can access any element of the array at any time randomly.
  2. We can easily sort the array either in ascending or in descending order by writing a few lines of code.
  3. We can traverse each and every element of the array easily.
  4. Code optimization is easy.

What are the disadvantages of the array?

In array, there is only one disadvantage, and that is because of its fixed size that we have set the size at the time of declaration, and we can’t exceed that limit in our program, therefore it is not dynamic in nature as a linked list.

I hope, you all have learned a lot about arrays and want to implement the same, must be excited about the coming modules.

Stay connected, keep practicing, Happy Leaning!