What is Array in C, Definition, Types, Uses | PW Skills

By | October 20, 2023

Varun Saharawat is a seasoned professional in the fields of SEO and content writing. With a profound knowledge of the intricate aspects of these disciplines, Varun has established himself as a valuable asset in the world of digital marketing and online content creation.


What are the Arrays in C

An array in C is the collection of similar data items together in a contiguous memory location. You can locate each element with the help of its index. In the array, the index starts at 0 for the first elements and then progresses one by one. The elements start filling from the left to the right until the size of the data items.

You can easily store and manipulate large amounts of data stored in a C array. You can store different data types, such as int, float, char, double, etc. You can also store user-defined data types such as pointers, structures, etc. 

What Is An Array In C?

An array is a collection of similar types of data items at a contiguous memory location. However, the size of the array is fixed, and you need to allot its size at the time of declaration.

1 4 8 25 2 17

The elements in this array are 1, 4, 8, 25, 2, and 17, and they are stored at indexes starting from 0 to 5 (size-1). However, it is essential to declare the data types of the elements stored in the array while declaring. The basic syntax for declaring an array in C is given here.

                                                              Data_type  array_name[ array_size ];

Let us name our array “arr” and declare it using the above syntax. The data type of our array elements will be integer, and the size is 6. 

1 4 8 25 2 17

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

           

In C language, we can declare an array by first naming the data_type of the items stored in the array. Keep in mind that the data types of all the elements inside the array need to be similar. Now, name your array using the naming guidelines for C with the size of the array. When we declare an array in C, our compiler will continuously allocate that memory block to the array.

Why do we need Arrays?

Array stores an extensive collection of similar data types. We have only three variables, and then we can easily store them using separate names like int var1, int var2, and int var3, but what if we have an extensive collection of these variables? Then, we cannot assign separate names to each of them, as it would be tedious and time-consuming.

Here comes the use of arrays in C, where we can store multiple data type values in a contiguous memory block. Hence, we can use an array in C when we are working with a large number of similar items. 

Accessing Elements in Arrays 

You can easily access elements in an Array using their index. Suppose we want to access the element at the third index of our array. Then, we can easily do it using arr[2], which is size-1. 

Access Elements in an array
int arr[6] = {1, 4, 8, 25, 2, 17};

printf (“%d”, arr[2]); // Accessing the third element at index 2, which is 8

Also, you can quickly iterate through arrays with loops and conditional statements. It allows quick and easy retrieval of data items. 

Array in C Initialization and Declaration

You need to initialise your array with some initial value at the time of declaration. This is done because when declaring arrays in C, they contain garbage values inside, which can alter our desired output. However, the easiest way to initialise the array is using the method below.

Array initialization using simplest approach
arr [0] = 1;  // initialising array in C

arr [1] = 4;

arr [2] = 8;

arr [3] = 25;

arr [4] = 2;

arr [5] = 17;

Hence, we need to initialise our array with some starting value. There are a few methods for declaring and initialising our array. 

1. Initializing Array In C During Declaration

Let us initialise our array with a proper size and elements inside. This list contains the elements enclosed within curly braces {}. 

Data_type name_of_array [ size ] = { value1, value2,……,valueN};

Array in C
int arr[6] = { 1, 4, 8, 25, 2, 17}

2. Initializing Array In C Without Size

Let us initialise our array without a proper predefined size and elements inside. This list contains the elements enclosed within curly braces {}. In this method, the compiler automatically detects the size of the array.

Data_type name_of_array [ ] = { value1, value2,……,valueN};

Array without size
int arr[ ] = { 1, 4, 8, 25, 2, 17}

3. Declaring Array in C with Loops

In this declaration method, we will use the loops to declare our array. We will use the loop to assign values to each element and insert them in our array.

Array using loops
for( int i=0 ; i<N;  i++){
name_of_array[ i ] = value i;}

Let us understand this with the help of the example we took above. Suppose we have six elements: 1, 4, 8, 25, 2, 17. We want to insert them in a loop.

Array in C
#include <iostream>

int main() {

   int elements[] = {1, 4, 8, 25, 2, 17};

    int arrSize = sizeof (elements) / sizeof (elements[0]);

 // Initializing array elements using for loop

   for (int i = 0; i < arrSize; i++) {

       elements[i] = arr[i];

    }

    cout << “Array elements after initialization using for loop:” <<endl;

   for (int i = 0; i < arraySize; ++i) {

        cout << elements[i] << ” “;

    }

    cout <<endl;

    return 0;

}

Example of Array in C

Let us take an example to understand the working of Array in C using a simple programming problem. We will use the naive sorting method to sort the elements inside of our array.

Sorting in Array
#include<stdio.h>    

void main ()  

{   

     int i, j,temp;

     int a[6] = {1, 4, 8, 25, 2, 17 }; 

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

        {    

            for(j = i+1; j<6; j++)    

      {    

           if(a[j] > a[i])    

             {    

                temp = a[i];    

                a[i] = a[j];    

                a[j] = temp;     

            }     

        }     

    }     

   printf (“Printing Sorted Element List …\n”);    

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

    {    

        printf (“%d\n”,arr[i]);    

     }    

        }     

Update Element inside an Array 

Suppose we want to update any element at a given index inside our array, then we simply need to use an assignment operator with the given syntax.

                            Name_of_array [ i ] = updated_value;

Update Elements in an Array 
arr [ 0 ] = 9;

This will update the element at the index 0 with a new value i,e. 9.

Types of C Array

There are mainly two types of C array given here.

1. One-Dimensional Array

This is also known as a 1-D array as it is only having one dimension.

We can easily declare a one-dimensional array using the same syntax we learned above.

name_of_Array [ size ];

2. Multi-Dimensional Array

This is also known as a multidimensional array because they have more than one dimension. We can have N- D arrays in C. Let us check the syntax for the N-D array.

name_of_Array [ size ] [ size2 ];

Recommended Course

Advantages of using Array in C

Arrays are used to store a large set of similar data items in a contiguous memory blocks. Let us check the advantages of the array in c.

  • With the help of an array we can store a large amount of data in a contiguous memory location.
  • Random access to any elements is possible inside our array using their index.
  • We can easily traverse our array using any loops available.
  • As the elements are stored in a contiguous memory location, there are no overhead costs involved. Hence, they are memory efficient.

Disadvantages of using Array in C

There are also a few shortcomings of using an array in C. Some of them we will check below.

  • Array in c has a fixed size, therefore we need to specify the size of the array at the time of declaration.
  • Using an array leads to memory wastage when you declare a size more than the actual required.
  • Insertion and deletion in an array are difficult. There are many other efficient data elements for this purpose. 
  • Also, for searching any elements in an unsorted array we need to traverse the whole array.

FREQUENTLY ASKED QUESTIONS (FAQs)

What is the use of a C array in programming?

C arrays are used for storing a large collection of similar data types elements in a contiguous memory location.

How can we access any elements inside an array?

We can simply access any elements inside our C array using array_name [ index]. You need to fill in the value of the index you want to access.

Is the array good for insertion and deletion?

Insertion in an array takes a large time complexity and is inefficient. However, there are many other data structures with efficient searching, sorting and insertion, such as hash tables, binary search trees, etc.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.