-2
vector<string> printPath(int m[MAX][MAX], int n) {
// Your code goes here
string curr_path;
path_finder((int*)m,0,0,curr_path,n);
return ans;}
//`void path_finder(int *m,int i,int j,string &curr_path,int n)
{
     if(m[i][j]==0)return;}`

//error:`prog.cpp: In function void path_finder(int*, int, int, std::__cxx11::string&, int):
prog.cpp:44:15: error: invalid types int[int] for array subscript
      if(m[i][j]==0)return;`

hello above mentioned are the code-error can anyone explain me how to pass a 2-D array as a refrence

ankitbeniwal
  • 435
  • 6
  • 18
  • Please have a look at this question: https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – Hussein Jaber Oct 09 '20 at 17:53
  • 1
    Simply use a [`std::array,MAX> m`](https://en.cppreference.com/w/cpp/container/array) instead of a raw array in c++. – πάντα ῥεῖ Oct 09 '20 at 17:56
  • 1
    Since this is a school assignment: What are you allowed to change? Everything? What's the current signature of `path_finder`? – Ted Lyngmo Oct 09 '20 at 17:57
  • Its is not a school Assignment I am using Leetcode I am given only PrintPath Function – sampras dsouza Oct 09 '20 at 18:01
  • Ok, so you can't change it to [this](https://godbolt.org/z/GanhMz)? – Ted Lyngmo Oct 09 '20 at 18:02
  • If you know the rows and cols before hand then you can pass it by reference, `template vector printPath(int (&m)[rows][cols], int n) { }` – Kunal Mukherjee Oct 09 '20 at 18:05
  • /tmp/ccYzlivY.o: In function `main': 1c194ecf4493d1b6d3ef813c33cfeff8.cpp:(.text+0xf0): undefined reference to `printPath[abi:cxx11](int (*) [5], int)' collect2: error: ld returned 1 exit status. getting this error @TedLyngmo – sampras dsouza Oct 09 '20 at 18:08
  • Does this answer your question? [Passing a 2D array to a C++ function](https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function) – Silidrone Oct 09 '20 at 18:09
  • Nope I dont know the size of Matrix – sampras dsouza Oct 09 '20 at 18:10
  • Yes you do. `MAX` defines the size. – Ted Lyngmo Oct 09 '20 at 18:14
  • Where do you get the error message from? A non-compliant C++ compiler? That's not the signature of the function I made. – Ted Lyngmo Oct 09 '20 at 18:15
  • Nope from the official compiler provided by the website – sampras dsouza Oct 09 '20 at 18:20
  • Well, in that case you didn't look at my code carefully enough OR you aren't allowed to change the signature of the `printPath` function. You didn't answer my question if it was allowed or not. Note: I didn't include all the arguments that you didn't explain. I just focused on the 2D array. What prevents you from making the `path_finder` function's signature `(int m[MAX][MAX], ...)`? – Ted Lyngmo Oct 09 '20 at 18:22

2 Answers2

0

You just require to pass a simple pointer to the function. Suppose you have an array like this :

int my_buff[4][5];

and you want to check or change cells of that. You can use a pointer likes below :

void change( int *buff)
{
    *(buff+1+5*2)=55; // Will change my_buff[2][1] to 55
}
mhheydarchi
  • 59
  • 1
  • 4
  • I agree with @HolyBlackCat - and you could save the answer by making it a 1D array of `int my_buff[4*5];` instead - if that's allowed. – Ted Lyngmo Oct 09 '20 at 22:59
0

The answer given by user mhheydarchi should not be used. In C++ I consider it as wrong.

Your question is:

How Pass 2-D Array to Function in CPP

The answer is:

If you want to pass 2d arrays to a function there is a special syntax.

You can pass by reference or by pointer. The array dimensions must be compile time constants. That is a requirement from C++.

Please see:

constexpr size_t NumberOfRows = 3;
constexpr size_t NumberOfColumns = 4;

// Typedef for easier usage
using IntMatrix2d = int[NumberOfRows][NumberOfColumns];

//Solution 1 ------
// Pass by reference
void function1(int(&matrix)[NumberOfRows][NumberOfColumns])  {}

// Pass by pointer
void function2(int(*m)[NumberOfRows][NumberOfColumns])  {}

//Solution 2 ------
// Pass by reference
void function3(IntMatrix2d& matrix) {}

// Pass by pointer 
void function4(IntMatrix2d* matrix) {}


int main()
{
    // Solution 1
    // Handwritten matrix. Dimension is compile time constant
    int matrix1[NumberOfRows][NumberOfColumns];

    // Pass by reference
    function1(matrix1);

    // Pass by pointer
    function2(&matrix1);

    // Solution 2 -----
    IntMatrix2d matrix2;

    // Pass by reference
    function3(matrix2);

    // Pass by pointer
    function4(&matrix2);

    return 0;
}

If you use using (or typedef) for your type definition, then it gets rather intuitive.

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29