Open In App

C Arrays

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc.

Arrays-in-C

Creating an Array in C

The whole process of creating an array in C language can be divided into two primary sub processes i.e.

1. Array Declaration

Array declaration is the process of specifying the type, name, and size of the array. In C, we have to declare the array like any other variable before using it.

C
data_type array_name[size];

The above statements create an array with the name array_name, and it can store a specified number of elements of the same data type.

Example:

C
// Creates array arr to store 5 integer values.
int arr[5];

When we declare an array in C, the compiler allocates the memory block of the specified size to the array name.

c-array-declaration

2. Array Initialization

Initialization in C is the process to assign some initial value to the variable. When the array is declared or allocated memory, the elements of the array contain some garbage value. So, we need to initialize the array to some meaningful values.

Syntax:

C
int arr[5] = {2, 4, 8, 12, 16};

The above statement creates an array arr and assigns the values {2, 4, 8, 12, 16} at the time of declaration.

We can skip mentioning the size of the array if declaration and initialisation are done at the same time. This will create an array of size n where n is the number of elements defined during array initialisation. We can also partially initialize the array. In this case, the remaining elements will be assigned the value 0 (or equivalent according to the type).

C
//Partial Initialisation
int arr[5] = {2, 4, 8};

//Skiping the size of the array.
int arr[] = {2, 4, 8, 12, 16};  

//initialize an array with all elements set to 0.
int arr[5] = {0};


Accessing Array Elements

Array in C provides random access to its elements, which means that we can access any element of the array by providing the position of the element, called the index.

Syntax:

The index values start from 0 and goes up to array_size-1. We pass the index inside square brackets [] with the name of the array.

C
array_name [index];

where, index value lies into this range - (0 ≤ index ≤ size-1).

access-array-element

Example:

C
#include <stdio.h>

int main() {

    // array declaration and initialization
    int arr[5] = {2, 4, 8, 12, 16};

    // accessing element at index 2 i.e 3rd element
    printf("%d ", arr[2]);

    // accessing element at index 4 i.e last element
    printf("%d ", arr[4]);

    // accessing element at index 0 i.e first element
    printf("%d ", arr[0]);
    return 0;
}

Output
8 16 2 

Update Array Element

We can update the value of array elements at the given index i in a similar way to accessing an element by using the array square brackets [] and assignment operator (=).

C
array_name[i] = new_value;

Example:

C
#include <stdio.h>

int main() {
    int arr[5] = {2, 4, 8, 12, 16};

    // Update the first value
    // of the array
    arr[0] = 1;
    printf("%d", arr[0]);
    return 0;
}

Output
1

C Array Traversal

Array Traversal is the process in which we visit every element of the array in a specific order. For C array traversal, we use loops to iterate through each element of the array.

c-array-traversal
Traversing An Array

Example:

C
#include <stdio.h>

int main() {
    int arr[5] = {2, 4, 8, 12, 16};
    
    // Print each element of
    // array using loop
    printf("Printing Array Elements\n"); 
    for(int i = 0; i < 5; i++){
        printf("%d ", arr[i]);
    }
    printf("\n"); 
    // Printing array element in reverse
    printf("Printing Array Elements in Reverse\n"); 
    for(int i = 4; i>=0; i--){
        printf("%d ", arr[i]);
    }
    return 0;
}

Output
2 4 8 12 16 

Size of Array

The size of the array refers to the number of elements that can be stored in the array. The array does not contain the information about its size but we can extract the size using sizeof() operator.

Example:

C
#include <stdio.h>

int main() {
    int arr[5] = {2, 4, 8, 12, 16};
    
    // Size of the array
    int size = sizeof(arr)/sizeof(arr[0]);
    printf("%d", size);
    return 0;
}

The sizeof() operator returns the size in bytes. sizeof(arr) returns the total number of bytes of the array. In an array, each element is of type int, which is 4 bytes. Therefore, we can calculate the size of the array by dividing the total number of bytes by the byte size of one element.

Note: This method only works in the scope in which the array is declared. Refer to this article to know more - Length of Array in C

Arrays and Pointers

Arrays and Pointers are closely related to each other such that we can use pointers to perform all the possible operations of the array. The array name is a constant pointer to the first element of the array and the array decays to the pointers when passed to the function.

C
#include <stdio.h>

int main() {

    int arr[5] = { 10, 20, 30, 40, 50 };
    int* ptr = &arr[0];
    
    // Address store inside 
    // name
    printf("%p\n", arr);
    
    // Print the address which
    // is pointed by pointer ptr
    printf("%p\n", ptr);
    return 0;
}

Output
0x7ffde73e54b0
0x7ffde73e54b0

To know more about the relationship between an array and a pointer, refer to this article - Pointer to an Arrays | Array Pointer.

Passing Array to Function

In C, arrays are passed to functions using pointers, as the array name decays to a pointer to the first element. So, we also need to pass the size of the array to the function.

Example:

C
#include <stdio.h>

// Functions that take array as argument
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
}

int main() {
    int arr[] = {2, 4, 8, 12, 16};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    // Passing array
    printArray(arr, n);
    return 0;
}

Output
2 4 8 12 16 

There are also other ways to pass array to functions. Refer to the article - Pass Array to Functions in C

Multidimensional Array in C

Multi-dimensional arrays in C are arrays that grow in multiple directions or dimensions. A one-dimensional array grows linearly, like parallel to the X-axis, while in a multi-dimensional array, such as a two-dimensional array, the elements can grow along both the X and Y axes.

Syntax

C
type array_name[size1][size2] .. [sizen];

Some commonly used multidimensional arrays are:

  • Two-Dimensional Array: It is an array that has exactly two dimensions. It can be visualized in the form of rows and columns organized in a two-dimensional plane.
  • Three-Dimensional Array: A 3D array has exactly three dimensions. It can be visualized as a collection of 2D arrays stacked on top of each other to create the third dimension.

Practice Array Problems

The following problems helps you to improve your efficiency in using C array:

Properties of C Arrays

C arrays have the following distinguishing properties:

  1. Fixed Size Collection
  2. Homogeneous Elements
  3. Indexing in Array
  4. Dimensions of Array
  5. Contiguous Storage
  6. Random Access
  7. Array name relation with pointer
  8. Bound Checking
  9. Array Decay

Refer to this article to know more - Properties of Array in C

Advantages of Array in C

The following are the main advantages of an array:

  1. Random and fast access of elements using the array index.
  2. Use of fewer lines of code as it creates a single array of multiple elements.
  3. Traversal through the array becomes easy using a single loop.
  4. Sorting becomes easy as it can be accomplished by writing fewer lines of code.

Disadvantages of Array in C

  1. C Arrays are not dynamic they only allow a fixed number of elements to be entered which is decided at the time of declaration.
  2. Insertion and deletion of elements can be costly since the elements are needed to be rearranged after insertion and deletion.

Article Tags :

Similar Reads