How To Pass A 2D Array As A Parameter In C?

How To Pass A 2D Array As A Parameter In C?

Passing multidimensional arrays, such as 2D arrays, to functions in C and C++ can indeed be a bit more complex than passing one-dimensional arrays or pointers. The reason for this is that in C and C++, the size of the second (and subsequent) dimensions must be specified, while the size of the first dimension can be left unspecified.

1) When both dimensions are available globally (either as a macro or as a global constant). 


#include <stdio.h>
const int M = 3;
const int N = 3;
 
void print(int arr[M][N])
{
    int i, j;
    for (i = 0; i < M; i++)
      for (j = 0; j < N; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr);
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 

2) When only the second dimension is available globally (either as a macro or as a global constant)


#include <stdio.h>
const int N = 3;
 
void print(int arr[][N], int m)
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < N; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr, 3);
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 

3) If the compiler is C99 compatible 



// The following program works only if your compiler is C99 compatible.
#include <stdio.h>
 
// n must be passed before the 2D array
void print(int m, int n, int arr[][n])
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int m = 3, n = 3;
    print(m, n, arr);
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 

Note: if the compiler is not C99 compatible, hence, we can use any of the listed methods to pass the variable-sized 2d array.

4) Using a single pointer 

We need to typecast the 2D array while passing to function.

#include <stdio.h>
void print(int *arr, int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        printf("%d ", *((arr+i*n) + j));
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int m = 3, n = 3;
 
    // We can also use "print(&arr[0][0], m, n);"
    print((int *)arr, m, n);
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 

5) Using the concept of a pointer to an array


#include <stdio.h>
const int M = 3;
 
 
void print(int (*arr)[M])
{
    int i, j;
    for (i = 0; i < M; i++)
    for (j = 0; j < M; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr);
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 

FAQ- How To Pass A 2D Array As A Parameter In C?

Q1. Can you pass a 2D array as a parameter in C?

Ans. In C, you can pass a 2D array to a function in two ways: by passing the entire array as an argument or by passing the array as a dynamic pointer to the function. The first method involves specifying the size of the second dimension while leaving the first dimension unspecified. The second method uses a dynamic pointer and is suitable for dynamically allocated 2D arrays.

Q2. How to go through a 2D array in C?

Ans. To access an element of a two-dimensional array, you need to provide both the row and column indices. In the given statement, it accesses the value of the element located in the first row (index 0) and the third column (index 2) of the “matrix” array.

Q3. What is the syntax for 2d array?

Ans. The syntax Of 2d Array is data_type[][] array_name; . In Java, to create a two-dimensional array, you specify the data type of the elements to be stored in the array, followed by two pairs of square brackets, and then provide the name of the array.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment