Passing a 3D Array to a Function in C

Last Updated : 8 Jul, 2026

A 3D array in C is a multi-dimensional array that stores elements in three dimensions: depth, rows, and columns. It can be passed to a function to process or manipulate all its elements efficiently.

  • A 3D array is accessed using three indices: array[depth][row][column], where each index represents a different dimension.
  • When passing a 3D array to a function, all dimens ions except the first must be specified in the function parameter so the compiler can calculate memory locations correctly.

Example: A 3D array representing the marks of students in multiple classes, where the first dimension represents the class, the second represents the student, and the third represents the subject.

Passing 3D Array as Parameter to a Function in C

We cannot directly pass a 3D array to a function just like we do one-dimensional arrays. Instead, we must pass the 3D array to function as a pointer. When doing so, the array undergoes array decay, losing information about its dimensions. Therefore, we must pass the dimensions of the array separately.

Syntax

functionType funcName(type (*arr)[cols][depth], int rows, int cols, int depth)

Here,

  • funcName: It is the name of the function.
  • arr: It is the pointer which points to the 3D array.
  • rows: It represents the number of 2D arrays.
  • cols: It represents the number of rows in each 2D array.
  • depth: It represents the number of columns in each 2D array.

C program to pass a 3D array to a function

The following program illustrates how we can pass a 3D array to a function in C.

C
#include <stdio.h>

// Function to print the elements of a 3D array
void printArray(int arr[][3][3], int rows, int cols,
                int depth)
{
    printf("Elements of the 3D array:\n");
    // Loop through each element in the 3D array
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            for (int k = 0; k < depth; ++k) {
                // Print each element
                printf("%d   ", arr[i][j][k]);
            }
            // Printing a new line at the end of each column
            printf("\n");
        }
        // Printing a new line at the end of each row
        printf("\n");
    }
}

int main()
{
    // Initialize the 3D array with fixed sizes
    int arr[3][3][3] = {
        { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } },
        { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } },
        { { 190, 200, 210 },
          { 220, 230, 240 },
          { 250, 260, 270 } }
    };

    // Declare the dimensions of the array
    int rows = 3;
    int cols = 3;
    int depth = 3;

    // Pass the 3D array to the function
    printArray(arr, rows, cols, depth);

    return 0; // Return 0 to indicate successful execution
}

Output
Elements of the 3D array:
10   20   30   
40   50   60   
70   80   90   

1   2   3   
4   5   6   
7   8   9   

190   200   210   
220   230   240   
250   260   270   


Comment