Arrays

Declaring, initializing, and accessing elements of arrays. Working with single and multi-dimensional arrays.


Multi-Dimensional Arrays in C

Introduction to Multi-Dimensional Arrays

In C programming, multi-dimensional arrays extend the concept of regular arrays to hold data in more than one dimension. Think of a regular array as a single row of values. A multi-dimensional array can be thought of as a grid, a cube, or even higher-dimensional structures. The most common type of multi-dimensional array is the two-dimensional array (also known as a matrix), and it's the one we'll focus on here.

Two-dimensional arrays are particularly useful for representing tables, matrices in mathematics, game boards (like chess or tic-tac-toe), or any data that naturally fits into a row-and-column format.

Declaring a 2D Array

To declare a 2D array in C, you use the following syntax:

data_type array_name[number_of_rows][number_of_columns];
  • data_type: The data type of the elements to be stored in the array (e.g., int, float, char).
  • array_name: The name you give to the array.
  • number_of_rows: The number of rows in the array.
  • number_of_columns: The number of columns in the array.

For example, to declare a 2D array of integers with 3 rows and 4 columns, you would write:

int my_array[3][4];

Initializing a 2D Array

You can initialize a 2D array during declaration in several ways:

1. Initializing with Literal Values

The most common method is to provide a list of values, grouped by rows:

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

In this example, my_array[0][0] will be 1, my_array[0][1] will be 2, and so on.

2. Initializing Sequentially

You can also initialize the array sequentially, but it's generally less readable:

int my_array[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

C will interpret this as filling the rows from left to right, top to bottom.

3. Partial Initialization

If you don't provide enough values to fill the entire array, the remaining elements are automatically initialized to 0:

int my_array[3][4] = {
    {1, 2},      // Row 0: 1, 2, 0, 0
    {5},         // Row 1: 5, 0, 0, 0
    {9, 10, 11}  // Row 2: 9, 10, 11, 0
};

Accessing Elements in a 2D Array

To access individual elements in a 2D array, you use the row and column indices, enclosed in square brackets. Remember that array indices in C start at 0.

element = array_name[row_index][column_index];

For example, to access the element in the second row (index 1) and third column (index 2) of my_array, you would write:

int value = my_array[1][2]; // value will be 7 in the first example.

You can also use this syntax to modify the value of an element:

my_array[0][0] = 100; // Change the element in the first row, first column to 100.

Example using Loops

Accessing and manipulating elements in a 2D array is commonly done using nested loops. The outer loop typically iterates through the rows, and the inner loop iterates through the columns.

#include <stdio.h>

int main() {
    int my_array[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // Print all elements of the array
    for (int i = 0; i < 3; i++) { // Iterate through rows
        for (int j = 0; j < 4; j++) { // Iterate through columns
            printf("my_array[%d][%d] = %d\n", i, j, my_array[i][j]);
        }
    }

    return 0;
}