Array

Last Updated: Jan 2026


So till now we have enough knowledge that are required to start Derived Data Types.Let's start with Arrays.

Arrays

An array is a collection of elements of the same type stored at contiguous memory locations enclosed in curly brackets . It is use to store multiple values of the same type in a single variable.


Type of Arrays

  • One-D Array
  • Two-D Array
  • Multi-D Array

One-D Array

A one-dimensional array is a linear collection of elements of the same type stored at contiguous memory locations.

In C, we can declare an array using the following syntax:

data_type array_name[size];

data_type array_name[size] = {value1, value2, value3, ...}; // explicit size

data_type array_name[] = {value1, value2, value3, ...}; // size is inferred
int arr[5] = {1, 2, 3, 4, 5};

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

In this example, arr is an array of integers with a size of 5.So lets try to understand contiguous memory locations.

Suppose address of arr[0] is 0x1000, arr[1] is 0x1004, arr[2] is 0x1008 and so on.This is the contiguous memory locations.

In C, we can declare an array using the following syntax:

data_type array_name[size];

data_type array_name[size] = {value1, value2, value3, ...}; // explicit size

data_type array_name[] = {value1, value2, value3, ...}; // size is inferred
int arr[5] = {1, 2, 3, 4, 5};

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

In this example, arr is an array of integers with a size of 5.So lets try to understand contiguous memory locations.

Suppose address of arr[0] is 0x1000, arr[1] is 0x1004, arr[2] is 0x1008 and so on.

Value Access

We can access the value of an array element using the following syntax:

array_name[index];

Example

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    // Value of Array
    printf("The value of arr[0] is %d\n", arr[0]);
    printf("The value of arr[1] is %d\n", arr[1]);
    //Address of Array
    printf("The address of arr[0] is %p\n", &arr[0]);
    printf("The address of arr[0] is %p\n", &arr[1]);

    return 0;
}

Note: It is just like a vector in mathematics.


Two-D Array

A array of arrays is a collection of one-dimensional arrays stored at contiguous memory locations.

Syntax

data_type array_name[row][column];

data_type array_name[][] = {{value1, value2, value3, ...}, {value1, value2, value3, ...}, ...};

Syntax For Values Access

array_name[row][column];

Example

#include <stdio.h>

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    // Value of Array
    printf("The value of arr[0][0] is %d\n", arr[0][0]);
    printf("The value of arr[0][1] is %d\n", arr[0][1]);
    printf("The value of arr[1][0] is %d\n", arr[1][0]);
    printf("The value of arr[1][1] is %d\n", arr[1][1]);
    // Address of Array
    printf("The address of arr[0][0] is %p\n", &arr[0][0]);
    printf("The address of arr[0][1] is %p\n", &arr[0][1]);
    printf("The address of arr[1][0] is %p\n", &arr[1][0]);
    printf("The address of arr[1][1] is %p\n", &arr[1][1]);

    return 0;
}

Note: It is just like a matrix in mathematics where we have 2 rows and 3 columns.


Multi-D Array

A array of arrays of arrays is a collection of two-dimensional arrays stored at contiguous memory locations.

Syntax

data_type array_name[row][column][depth];

data_type array_name[][][] = {{{value1, value2, value3, ...}, {value1, value2, value3, ...}, ...}, {{value1, value2, value3, ...}, {value1, value2, value3, ...}, ...}, ...};

Syntax For Values Access

array_name[row][column][depth];

Example

#include <stdio.h>
int main() {
   int arr[2][3][4] = {{{1, 2, 3, 4},
                     {5, 6, 7, 8},
                     {9, 10, 11, 12}},
                    {{13, 14, 15, 16},
                     {17, 18, 19, 20},
                     {21, 22, 23, 24}}};
    // Value of Array
    printf("The value of arr[0][0][0] is %d\n", arr[0][0][0]);
    printf("The value of arr[0][0][1] is %d\n", arr[0][0][1]);
    printf("The value of arr[0][0][2] is %d\n", arr[0][0][2]);
    // Address of Array
    printf("The address of arr[0][0][0] is %p\n", &arr[0][0][0]);
    printf("The address of arr[0][0][1] is %p\n", &arr[0][0][1]);
    printf("The address of arr[0][0][2] is %p\n", &arr[0][0][2]);

    return 0;
}

Note: It is just like a 3D matrix in mathematics where we have 2 rows, 3 columns and 4 layers.


typedef with Arrays

Creating aliases for array types simplifies parameter passing and fixed-size buffer definitions.

#include <stdio.h>

// Define 'Vector3D' as an alias for an array of 3 doubles
typedef double Vector3D[3];

// Define 'String50' as an alias for a 50-character buffer
typedef char String50[50];

int main(void) {
    Vector3D position = {1.0, 2.5, 0.0};
    String50 name = "Sadhu";

    printf("[%.1f, %.1f, %.1f]\n", position[0], position[1], position[2]);
    printf("Name: %s\n", name);

    return 0;
}

Size Calculation Of Arrays

sizeof(array_name);

Note: It returns the size of the array in bytes.

Example

#include <stdio.h>

int main() {
    int arr[3];
    printf("The size of arr is %d bytes\n", sizeof(arr));

    return 0;
}

Length Calculation Of Arrays

sizeof(array_name)/sizeof(array_name[0]);

Note: It returns the length of the array.

Example

#include <stdio.h>

int main() {
    int arr[3];
    printf("The length of arr is %d\n", sizeof(arr)/sizeof(arr[0]));

    return 0;
}

Input Taking In Arrays

For taking input in arrays, we can use the following syntax, Suppose array is 1D array:

scanf("%d", &array_name[index]);

Example of Integer Array:

#include <stdio.h>

int main() {
    int arr[3];
    for (int i = 0; i < 3; i++) {
        scanf("%d", &arr[i]);
    }

    // Print Array
    for (int i = 0; i < 3; i++) {
        printf("The value of arr[%d] is %d\n", i, arr[i]);
    }

    return 0;
}

Example of String Array:

#include <stdio.h>

int main() {
    char arr[3][10];
    for (int i = 0; i < 3; i++) {
        scanf("%s", arr[i]);
    }

    // Print Array/Traverse Array
    for (int i = 0; i < 3; i++) {
        printf("The value of arr[%d] is %s\n", i, arr[i]);
    }

    return 0;
}

Note: The size of the array should be equal to the number of elements to be taken as input.


Updating Array Elements

Syntax for updating 1D Array elements:

array_name[index] = value;

Example:

#include <stdio.h>

int main() {
    int arr[3];
    for (int i = 0; i < 3; i++) {
        scanf("%d", &arr[i]);
    }

    for (int i = 0; i < 3; i++) {
        printf("The value of arr[%d] is %d\n", i, arr[i]);
    }

    printf("Enter the index of the element you want to update: ");
    int index;
    scanf("%d", &index);

    printf("Enter the new value: ");
    int value;
    scanf("%d", &value);

    arr[index] = value;

    for (int i = 0; i < 3; i++) {
        printf("The value of array after updating arr[%d] is %d\n", i, arr[i]);
    }

    return 0;
}

Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR