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

Array: Definition, Types, Explanation, and Examples

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?

A one-dimensional array stores elements of the same data type in continuous memory locations. It works like a simple list, and programmers access each value using an index that starts from zero.

The array size must be known at compile time, and once declared, it cannot be changed.

Basic Array Declaration

int arr[50];

This declares an array named arr that can store 50 integers. Instead of declaring 50 separate variables, an array allows you to manage a collection of related values under a single name, making your code cleaner and more efficient.

Characteristics of an Array

  • Zero-based Indexing: Array indices start at 0 and go up to size - 1.
  • Direct Access: Any element can be accessed directly using its index like arr[index].
  • Base Address: The memory address of the first element (arr[0]) is called the base address of the array.
  • Fixed Size: The size of an array is determined at compile time and cannot be changed during program execution.

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 is essentially an "array of arrays." It organizes data in a grid of rows and columns, making it ideal for representing matrices, tables, or game boards.

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

C supports arrays with more than two dimensions. A multi-dimensional array is essentially an array of arrays of arrays (and so on). Three-dimensional arrays are common for representing volumetric data, color images (with RGB channels), or time-series of matrices.

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

Key Points to Remember About Arrays

  • Memory Layout: Multi-dimensional arrays are stored in row-major order in C (rightmost index varies fastest).
  • Total Elements: For an array declared as arr[d1][d2][d3], total elements = d1 × d2 × d3.
  • Bounds Checking: C does not perform array bounds checking. Accessing outside array bounds leads to undefined behavior.
  • Fixed Size: All array dimensions must be constant expressions known at compile time.
  • Performance: Arrays provide O(1) access time to any element, making them very efficient for random access.

Conclusion

Arrays provide a structured and efficient way to store fixed-size collections of data in continuous memory. Their direct access capability, simple syntax, and predictable performance make them essential for numerical computing, matrices, tables, and many real-world applications. Understanding 1D, 2D, and multi-dimensional arrays builds a strong foundation for mastering data structures and memory management in C programming.