Home » Data Structure » Array: Definition, Types, Explanation, and Examples

Array: Definition, Types, Explanation, and Examples

Last updated:

Support this post with a reaction:

3,119 views

An array is a fundamental data structure that helps you store multiple values of the same data type in a single, organized block of memory. It allows programmers to efficiently access elements using an index, and they widely use it to store lists, tables, matrices, and structured collections of data. Arrays make programs cleaner, faster, and easier to manage, especially when handling large datasets.

What Is an Array in Data Structure?

An array is a linear data structure that stores multiple elements of the same data type in contiguous memory locations. Each element is identified using an index, which generally starts from 0.

Arrays allow programmers to store and manage multiple related values using a single variable name instead of declaring separate variables for every value.

For example, if you want to store the marks of five students, you can use an array instead of creating five different variables.

Example:

int marks[5] = {85, 90, 78, 92, 88};

Here:

  • int specifies the data type.
  • marks is the array name.
  • 5 represents the number of elements.
  • {85, 90, 78, 92, 88} represents the stored values.

Each element can be accessed using its index.

printf("%d", marks[0]); 

Output: 85

printf("%d", marks[2]); 

Output: 78

How Does an Array Work in Memory?

An array stores its elements in contiguous memory locations. This means that the elements are placed next to each other in memory.

Consider the following array:

int arr[5] = {10, 20, 30, 40, 50};

Its logical representation is:

IndexElement
010
120
230
340
450

If an integer occupies 4 bytes, the memory locations will be separated by 4 bytes.

Characteristics of an Array

  1. Contiguous Memory Allocation: Array elements are stored in consecutive memory locations.
  2. Homogeneous Elements: All elements belong to the same array element type.
  3. Zero-Based Indexing: In C, the first element is accessed using index 0.
  4. Direct Access: Elements can be accessed directly using their index.
  5. Fixed Array Size: A conventional array cannot be resized after its creation. C also supports variable-length arrays in applicable C versions and contexts, but they still cannot be resized after declaration.
  6. Easy Traversal: Loops can be used to access all elements systematically.
  7. Efficient Searching and Sorting: Arrays support several searching and sorting algorithms.

Different Ways to Store Elements in an Array

Method 1: Individual Assignment

int arr[50];
arr[0] = 6;
arr[1] = 22;
arr[2] = 30;
// ... and so on up to arr[49]

Method 2: Initializer List at Declaration

int array[5] = {10, 12, 44, 21, 54};

Method 3: Reading Input at Runtime

int arr[50];
for(int i = 0; i < 50; i++) {
    scanf("%d", &arr[i]);  // Read input into each array element
}

Accessing Array Elements

Elements can be accessed individually or through a loop.

// Access single element
printf("%d", arr[3]);  // Prints the 4th element

// Access all elements
for(int i = 0; i < 50; i++) {
    printf("%d ", arr[i]);  // Prints all 50 elements
}

Types of Arrays in C

1. One Dimensional Array

A one-dimensional array in C is a linear collection of elements that all share the same data type and are stored in continuous memory locations. It works like a simple list where each value can be accessed using an index, starting from zero.

1D Array Declaration Syntax

data_type array_name[array_size];

Example: int marks[100] which declares an array that can store 100 integer values.

Initialization Methods of One Dimensional Array

Individual Initialization:

int scores[5];
scores[0] = 85;
scores[1] = 92;
scores[2] = 78;
scores[3] = 90;
scores[4] = 88;

Compact Initialization:

int scores[5] = {85, 92, 78, 90, 88};

Note: If you provide fewer values than the array size, remaining elements are automatically initialized to zero:

int arr[10] = {1, 2, 3};  // arr[3] to arr[9] are set to 0

1D Array Implementation in C

#include <stdio.h>

int main() {
    int numbers[50];
    int i;
    
    // Initialize array with values 50 through 99
    for (i = 0; i < 50; i++) {
        numbers[i] = i + 50;
    }
    
    // Display first 5 elements
    printf("First 5 elements of the array:\n");
    for (i = 0; i < 5; i++) {
        printf("numbers[%d] = %d\n", i, numbers[i]);
    }
    
    return 0;
}

Output

First 5 elements of the array:
numbers[0] = 50
numbers[1] = 51
numbers[2] = 52
numbers[3] = 53
numbers[4] = 54

2. Two Dimensional Array (Matrix)

A two-dimensional array (2D array) is an array in which elements are organized in rows and columns. It can be considered an array of arrays.

A 2D array is commonly used to represent matrices, tables, marksheets, game boards and other grid-based data.

A Two Dimensional Array uses the two subscripts for declaring the elements of the Array.

2D Array Declaration Syntax

data_type array_name[rows][columns];

Example: int matrix[5][4]; creates a 5×4 grid capable of storing 20 integers (5 rows × 4 columns).

Initialization Methods of Two Dimensional Array

Method 1: Row-wise Initialization

int matrix[3][3] = {
    {1, 2, 3},     // Row 0
    {4, 5, 6},     // Row 1
    {7, 8, 9}      // Row 2
};

Method 2: Flattened Initialization

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

Important: For partial initialization, unspecified elements are set to zero.

2D Array Implementation in C

#include <stdio.h>

int main() {
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    
    printf("Matrix elements:\n");
    for(int row = 0; row < 3; row++) {
        for(int col = 0; col < 4; col++) {
            printf("matrix[%d][%d] = %d\t", row, col, matrix[row][col]);
        }
        printf("\n");
    }
    
    return 0;
}

Output

Matrix elements:
matrix[0][0] = 1    matrix[0][1] = 2    matrix[0][2] = 3    matrix[0][3] = 4
matrix[1][0] = 5    matrix[1][1] = 6    matrix[1][2] = 7    matrix[1][3] = 8
matrix[2][0] = 9    matrix[2][1] = 10   matrix[2][2] = 11   matrix[2][3] = 12

3. Multi-Dimensional Array

A multidimensional array is an array that contains more than two dimensions. It is useful for storing complex data that needs multiple levels of indexing.

A three-dimensional array (3D array) is a common example of a multidimensional array.

It can be visualized as a collection of 2D arrays arranged in different layers.

3D Array Declaration Syntax

data_type array_name[depth][rows][columns];

Example: int cube[3][3][3]; creates a 3×3×3 cube capable of storing 27 integers.

Initialization of 3D Array

// A 2×3×4 array (2 layers, each with 3 rows and 4 columns)
int data[2][3][4] = {
    {   // Layer 0
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    },
    {   // Layer 1
        {13, 14, 15, 16},
        {17, 18, 19, 20},
        {21, 22, 23, 24}
    }
};

3D Array Implementation in C

#include <stdio.h>

int main() {
    int arr[2][3][2];  // 2 layers × 3 rows × 2 columns = 12 elements
    int i, j, k;
    int value = 1;
    
    // Initialize with sequential values
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            for(k = 0; k < 2; k++) {
                arr[i][j][k] = value++;
            }
        }
    }
    
    printf("3D Array Contents:\n");
    
    // Display the 3D array with [i][j][k] format
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 3; j++) {
            for(k = 0; k < 2; k++) {
                printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
            }
        }
    }
    
    return 0;
}

Output

3D Array Contents:
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[0][2][0] = 5
arr[0][2][1] = 6
arr[1][0][0] = 7
arr[1][0][1] = 8
arr[1][1][0] = 9
arr[1][1][1] = 10
arr[1][2][0] = 11
arr[1][2][1] = 12

Basic Operations Performed on Arrays

An array supports several operations for storing, accessing and modifying data.

1. Traversal

Traversal means visiting each element of an array one by one.

int arr[5] = {10, 20, 30, 40, 50};

for(int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
}

2. Insertion

Insertion means adding a new element at a particular position in an array.

For a simple fixed-size array, elements may need to be shifted to create space.

Example of Insertion:

Original: 10 20 30 40
Insert 25 at index 2
Result:   10 20 25 30 40

3. Deletion

Deletion means removing an element from a particular position.

After deletion, the subsequent elements are shifted to fill the empty position.

Original: 10 20 30 40 50
Delete element at index 2
Result:   10 20 40 50

4. Searching

Searching means finding whether a particular element exists in an array.

Two commonly used searching techniques are:

  • Linear Search
  • Binary Search (requires sorted data)

5. Updating

Updating means changing the value of an existing array element.

Example of Updating:

int arr[5] = {10, 20, 30, 40, 50};

arr[2] = 100;

Updated array:

10 20 100 40 50