# Passing a 2D array to a C++ function using reference

**URL:** <https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321>\
**Category:** general\
**Tags:** 2-d-array\
**Created:** [July 4, 2017, 6:15pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321 "2017-07-04T18:15:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashedcs](https://discuss.codechef.com/user_avatar/discuss.codechef.com/rashedcs/32/298254_2.png) [@rashedcs](https://discuss.codechef.com/u/rashedcs)\
**Post date:** [July 4, 2017, 6:15pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/1 "2017-07-04T18:15:00Z")

</div>

How to pass a 2D array to a c++ function using reference efficiently?  
I have tried but it show error .  
[**code :**](http://ideone.com/GrGJ52)

---

<div class="post-metadata">

**Author:** ![hruday968](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@hruday968](https://discuss.codechef.com/u/hruday968)\
**Post date:** [July 4, 2017, 6:23pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/2 "2017-07-04T18:23:32Z")

</div>

Instead of passing the array declare the array globally,then you can use the array where ever you want.

---

<div class="post-metadata">

**Author:** ![vijju123](https://discuss.codechef.com/user_avatar/discuss.codechef.com/vijju123/32/195207_2.png) [@vijju123](https://discuss.codechef.com/u/vijju123)\
**Post date:** [July 4, 2017, 7:05pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/3 "2017-07-04T19:05:57Z")

</div>

Passing 2-D array isnt as tough as you are making it out to be! I want to ask you, is it necessary for you to pass using pointers? If not, then simply use-

```
#include<bits/stdc++.h>
using namespace std;
 
int r, c=0;
void print(int a[][4])
{
for(size_t i=0; i<r; i++)
{
for(size_t j=0; j<c; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
 
int main()
{

cin>>r>>c;
int arr[r][4];
 
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
cin>>arr[i][j];
}
}
print(arr); //
return 0;
}

```

The few keypoints you need to know are-

1. For a 2-D array, number of columns MUST be known while passing.
2. Specifying rows is optional, and is usually avoided.
3. In your function, row and column aren’t declared in scope of that function. You need to pass the information of the row and column to the function as well.
4. Number of column must be a constant, not a variable. If its stored in a variable, it should be like

Also, refer these links-

> **[How to pass a 2D array as a parameter in C? - GeeksforGeeks](https://www.geeksforgeeks.org/pass-2d-array-parameter-c/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

> <https://stackoverflow.com/questions/10164263/how-to-pass-two-dimensional-array-of-an-unknown-size-to-a-function>

---

<div class="post-metadata">

**Author:** ![sudip\_95](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@sudip\_95](https://discuss.codechef.com/u/sudip_95)\
**Post date:** [July 4, 2017, 7:48pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/4 "2017-07-04T19:48:43Z")

</div>

[@vijju123](https://discuss.codechef.com/users/187411/vijju123), Is there any way to pass a 2D array if no parameter is known without declaring it globally?

---

<div class="post-metadata">

**Author:** ![vijju123](https://discuss.codechef.com/user_avatar/discuss.codechef.com/vijju123/32/195207_2.png) [@vijju123](https://discuss.codechef.com/u/vijju123)\
**Post date:** [July 4, 2017, 8:02pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/5 "2017-07-04T20:02:37Z")

</div>

Read the 2nd link I gave. The Q you have is one of the good follow up questions, so i made sure to answer it as well. The answer there states that its better to use vectors for this purpose. (Vector of vectors).

```auto
However, the standard library supplies std::vector container, that works very well for multi-dimension arrays: in your case, passing vector<vector<int> > &matrix would be the proper way of dealing with the task in C++.

```

---

<div class="post-metadata">

**Author:** ![sudip\_95](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@sudip\_95](https://discuss.codechef.com/u/sudip_95)\
**Post date:** [July 4, 2017, 8:04pm UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/6 "2017-07-04T20:04:26Z")

</div>

thanks! 🙂

---

<div class="post-metadata">

**Author:** ![bansal1232](https://discuss.codechef.com/user_avatar/discuss.codechef.com/bansal1232/32/28353_2.png) [@bansal1232](https://discuss.codechef.com/u/bansal1232)\
**Post date:** [July 5, 2017, 12:39am UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/7 "2017-07-05T00:39:00Z")

</div>

I told you many times that there exist some other section of clearing your doubt. But still you repeat the same process again and again. At least try to search by **google** , you can get plenty of information in one go.

Please take some tips from @vijju123 for how to use google or other websites like **geeksforgeeks, stakeoverflow, quora, mathstackexhange** etc. He is expert in this.

---

<div class="post-metadata">

**Author:** ![rashedcs](https://discuss.codechef.com/user_avatar/discuss.codechef.com/rashedcs/32/298254_2.png) [@rashedcs](https://discuss.codechef.com/u/rashedcs)\
**Post date:** [July 5, 2017, 5:19am UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/8 "2017-07-05T05:19:40Z")

</div>

Hey &bansal1232 , i have tried at least 3 hours in above code and searched in google. I post codechef forum, when i can not solve . Probably u did not show my code. I request u see my code and then comment me.

---

<div class="post-metadata">

**Author:** ![rajujnvgupta](https://discuss.codechef.com/user_avatar/discuss.codechef.com/rajujnvgupta/32/3653_2.png) [@rajujnvgupta](https://discuss.codechef.com/u/rajujnvgupta)\
**Post date:** [July 27, 2019, 3:57am UTC](https://discuss.codechef.com/t/passing-a-2d-array-to-a-c-function-using-reference/15321/9 "2019-07-27T03:57:29Z")

</div>

see this  
[http://www.cplusplus.com/forum/general/95826/](http://www.cplusplus.com/forum/general/95826/)
