Passing Multi-Dimensional Arrays as Function Arguments

You can pass multi-dimensional arrays to functions in much the same way as single-dimensional arrays, but with some additional considerations regarding size specification. Multi-dimensional arrays are typically used to represent matrices, tables, or grids. They can be passed to functions by specifying the array size in the function signature or using pointers.

Passing a 2D Array to a Function

When passing a multi-dimensional array to a function, you need to explicitly specify all but the first dimension in the function’s parameter list. For example:

void printArray(int arr[][4], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 4; j++) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

Key Point: In the example above, the second dimension (4) must be explicitly specified in the function’s parameter list.

Example Program with 2D Array

#include <iostream>

void printArray(int arr[][4], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 4; j++) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

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

    printArray(myArray, 3);

    return 0;
}

Why Do We Need to Specify the Second Dimension?

In C++, arrays are stored in contiguous memory, and when passing them to a function, only the pointer to the first element is passed. To correctly interpret the multi-dimensional structure, the compiler must know the size of each inner array (the second dimension). Without this information, it cannot calculate the correct memory offsets.

Passing 3D Arrays or Higher Dimensions

If you want to pass a three-dimensional array to a function, you must specify all dimensions except the first one:

void print3DArray(int arr[][3][4], int xSize) {
    for (int i = 0; i < xSize; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                std::cout << arr[i][j][k] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
}

Calling the function:

int my3DArray[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}
    }
};

print3DArray(my3DArray, 2);

Passing Multi-Dimensional Arrays by Reference

Another approach is to pass a multi-dimensional array by reference. This allows the compiler to deduce the size of all dimensions automatically:

void printArrayRef(const int (&arr)[3][4]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

Using Pointers for Multi-Dimensional Arrays

When working with dynamic arrays or unknown dimensions, you may use pointers to achieve similar results. However, handling pointers can be complex and should be used carefully to avoid memory issues.

Summary

Passing multi-dimensional arrays in C++ requires specifying all dimensions except the first. By doing this, the compiler can correctly interpret the memory layout and access array elements. When dealing with higher dimensions or dynamically allocated arrays, consider passing the array by reference or using pointers cautiously.