Passing a 2D Array as a Function Parameter in C and C++

Shriya Madan
The Startup
Published in
3 min readJul 8, 2020

Recently I encountered a problem in a company’s coding round which had a question of rotating a matrix in multiples of 90 degrees. The catch for me was not the algorithm or rotation of array but to “pass the 2-D array in a function”. The question is definitely a good one, but here I will scrutinize the passing of a matrix as an argument in C++.

I tried to figure it out during the contest but was unable to do it back then, obviously because of the limited time allotted for the completion of the challenge. So I immediately converted my code to C language.

Here’s a straightforward way to do it in C language:

void func(int m, int n, int arr[][n])       //function prototype
{
printf("%d", arr[0][0]); //accessing an element of the array
}

The main function would look like this:

int main() 
{ int m, n;
scanf("%d %d", &m,&n);
int arr[m][n];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
scanf("%d", &arr[i][j]);
func(m, n, arr); //function call

return 0;
}

As simple as it seems!

But a similar solution would throw errors in C++.

After the contest was over, I was very curious to find the problem in my code as I have been using C++ for a long time in coding competitions but didn’t clash with a problem of such kind earlier. I tried StackOverflow solutions and even asked my old training teacher to help me out with it. He told the same C solution we already discussed above. Then I contacted one of my favorite YouTubers, for the same. He told me to use vectors.

So, I will discuss below all viable solutions I found out:

It is pretty easy if the number of rows and columns are constants or defined using macros.

But, if the array is dynamically allocated, then you may proceed with me:

1. Using Single pointer to typecast the 2D array:

void func(int *arr, int m, int n)         //function prototype
{
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
cout<<*((arr+i*n) + j)<<" "; //printing each element
}
cout<<"\n";
}
}

int main()
{
int rows = 3, cols = 3;
int arr[rows][cols] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

func((int *)arr, rows, cols); //function call
return 0;
}

*(Array+i) is same as Array[i]

For 2-D array,
Array[row][col] = *(Array[row] + col) = *(*(Array+row) + col)

2. Using Double pointers(pointer to pointer):

void func(int **arr, int row, int col)        //function prototype
{
for (int i=0; i<row; i++)
{
for(int j=0 ; j<col; j++)
{
cout<<arr[i][j]<<" "; //printing each element of array
}
cout<<"\n";
}
}
int main()
{
int row, colm;
cin >> row >> colm;
int** arr = new int*[row];

for(int i=0; i<row; i++)
arr[i] = new int[colm];

for(int i=0; i<row; i++)
for(int j=0; j<colm; j++)
cin >> arr[i][j];

func(arr, row, colm); //function call

return 0;
}

Arrays of pointers:

Pointers to pointers have a few uses. The most common use is to dynamically allocate an array of pointers:

int **array = new int*[10];

This allocates an array of 10 int pointers which are rows in the above code. It works just like a standard dynamically allocated array, except the array elements are of type “pointer to integer” instead of an integer.

for(int count=0; count<10; count++)
array[count] = new int[5]; //these are our columns

We can then access our array as usual,

array[6][4] = 5;

3. Using 2D vectors (the way I would recommend):

Function prototype:

void func(vector < vector<int> > matrix)   

Function call:

func(twoDvector);

The two main things you need to notice in all the above instances are the function prototype and the function call.

I struggled to understand this problem and so thought about writing it in a structured manner. If anyone has further queries, feel free to comment below.

Hope it helps!

--

--