How to Pass a 2D Array as a Function Arguments in C?

What is a 2D Array in C?

A 2D array in C is an array of arrays. Think of it like a table with rows and columns. Each element is identified by two indices: one for the row and one for the column.

Syntax of a 2D array:

data_type array_name[rows][columns];

Example:

int matrix[3][4]; // A 2D array with 3 rows and 4 columns

Memory Layout of 2D Arrays in C

2D arrays in C are stored in row-major order. This means the entire first row is stored first in memory, followed by the second row, and so on.

So, if you write:

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

It is stored as: 1, 2, 3, 4, 5, 6 in memory.

Why Pass a 2D Array to a Function?

In real programs, you often need to break your logic into smaller functions for clarity and reuse. Instead of writing all code in main(), you can write a separate function to process, modify, or print a 2D array.

To do this, you need to pass the array as an argument to the function.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

Ways to Pass a 2D Array to a Function in C

Method 1: Specify the Column Size

You declare the number of columns explicitly in the function signature.

Syntax:

void functionName(dataType array[][COL_SIZE], int rows);

Example:

#include <stdio.h>
 
void printQuizMatrix(int quiz[][3], int rows) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < 3; j++)
            printf("Quiz[%d][%d] = %d\n", i, j, quiz[i][j]);
}
 
int main() {
    int quizScores[2][3] = {{80, 85, 90}, {75, 70, 95}};
    printQuizMatrix(quizScores, 2);
    return 0;
}

Output:

Quiz[0][0] = 80
Quiz[0][1] = 85
Quiz[0][2] = 90
Quiz[1][0] = 75
Quiz[1][1] = 70
Quiz[1][2] = 95

This C program demonstrates how to pass a 2D array to a function. The printQuizMatrix function takes a 2D array with 3 columns and prints its elements. In main(), a 2×3 array quizScores is defined and passed to the function. The program prints each element with its row and column index.

Method 2: Using Pointer to an Array

You pass a pointer to an array of fixed-size columns.

advertisement

Syntax:

void functionName(dataType (*array)[COL_SIZE], int rows);

Example:

#include <stdio.h>
 
void printCertificationMatrix(int (*cert)[3], int rows) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < 3; j++)
            printf("Certification[%d][%d] = %d\n", i, j, cert[i][j]);
}
 
int main() {
    int certificationScores[2][3] = {{91, 93, 95}, {88, 86, 84}};
    printCertificationMatrix(certificationScores, 2);
    return 0;
}

Output:

Certification[0][0] = 91
Certification[0][1] = 93
Certification[0][2] = 95
Certification[1][0] = 88
Certification[1][1] = 86
Certification[1][2] = 84

This C program prints a 2D array of certification scores. The printCertificationMatrix function takes a pointer to a 2D array and the number of rows. It uses nested loops to print each score with its row and column index. In main, the array has 2 rows and 3 columns, and the function prints all the values.

Method 3: Using Variable-Length Arrays (VLAs)

If your compiler supports C99 or later, you can use Variable Length Arrays to pass arrays with sizes determined at runtime:

Syntax:

void functionName(int rows, int cols, dataType array[rows][cols]);

Example:

#include <stdio.h>
 
void displayTestMatrix(int rows, int cols, int test[rows][cols]) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            printf("Test[%d][%d] = %d\n", i, j, test[i][j]);
}
 
int main() {
    int testScores[2][3] = {{68, 72, 74}, {80, 85, 89}};
    displayTestMatrix(2, 3, testScores);
    return 0;
}

Output:

Test[0][0] = 68
Test[0][1] = 72
Test[0][2] = 74
Test[1][0] = 80
Test[1][1] = 85
Test[1][2] = 89

This C program shows how to pass and print a 2D array using a function. The testScores array has two rows and three columns. The displayTestMatrix function takes this array and prints each element using nested loops. It shows how 2D arrays can be handled in functions using simple indexing.

Method 4: Using Double Pointers to Pass a 2D Array in C

Using a double pointer (int **ptr) is not the same as a regular 2D array. This method is used when the 2D array is dynamically allocated, and the rows are created using malloc().

Syntax:

void functionName(int **array, int rows, int cols);

Example:

#include <stdio.h>
#include <stdlib.h>
 
void printMatrix(int **quiz, int rows, int cols) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            printf("Quiz[%d][%d] = %d\n", i, j, quiz[i][j]);
}
 
int main() {
    int rows = 2, cols = 3;
    int **quiz = (int **)malloc(rows * sizeof(int *));
 
    for (int i = 0; i < rows; i++) {
        quiz[i] = (int *)malloc(cols * sizeof(int));
        for (int j = 0; j < cols; j++) {
            quiz[i][j] = (i + 1) * (j + 10);  // Assigning quiz scores
        }
    }
 
    printMatrix(quiz, rows, cols);
 
    for (int i = 0; i < rows; i++) {
        free(quiz[i]);
    }
    free(quiz);
 
    return 0;
}

Output:

Quiz[0][0] = 10
Quiz[0][1] = 11
Quiz[0][2] = 12
Quiz[1][0] = 20
Quiz[1][1] = 22
Quiz[1][2] = 24

This C program shows how to create, use, and free a dynamic 2D array. It allocates memory for a 2×3 matrix using malloc. Each element is assigned a value based on its row and column. The printMatrix function prints the array using pointer access (quiz[i][j]). After printing, the program frees all allocated memory. This example demonstrates dynamic memory allocation for 2D arrays in C.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.