So I have a multi-dimensional array, and I want to pass a reference of it to another function so that the array is defined in the scope of the function, because the array is not a global variable.
If I can remember correctly, the syntax looks a little like this:
reset(int &Board[][])
I'm not sure what to put inside the indice boxes either... Does anyone have any suggestions. I'm going to look on google a bit as well right now.

Recommended Answers

All 9 Replies

try this code
void reset(int *board)
and in calling reset(arrayname)
in passing by raference we pass the address to the function as we do it in pointers i hope it will work.
you can not pass the reference of array because it gives error if you want to pass it's address you have to use pointers because logically an array's name is also a pointer so we can pass it to functio.

> I'm not sure what to put inside the indice boxes
Ed would recommend the size of the array as it's required for an array reference. ;)

reset(int (&Board)[2][3])

The parentheses are also required to bind the reference operator to the array instead of to the type of each element.

hai,
i am new one.
i am learning the c++ now.
it is such a interesting language.
the passing by reference is used to pass the arguments by addresses.
thanks..........
<snip>

So I'm still having a ton of issues with my functions that pass by reference. The compiler is giving me numerous errors with both the call and the function itself.
Here is the simplest function that passes by reference:

void Display(int* const pBoard)
{
     for (int row = 0; row <= 8; row++)
     {
          cout << endl;
          for (int column = 0; column <= 8; column++)
          {
              cout << pBoard[column][row];  // prints horizontally. 
          }
     }
}

Does anyone have some ideas what's wrong?

reset(int (&Board)[2][3])

I have never in over 20 years seen anyone code a function prototype like that. Arrays are ALWAYS passed by reference, it is not possible to pass an array by value. So this is all that is needed void reset(int Board[2][3]);

So I'm still having a ton of issues with my functions that pass by reference. The compiler is giving me numerous errors with both the call and the function itself.
Here is the simplest function that passes by reference:
Does anyone have some ideas what's wrong?

Yes --
1) pBoard is not a 2d array but a single dimension array.
2) you declared pBoard as a const, that means you can not change any of its values.

void Display(int Board[2][3])
{
     for (int row = 0; row <= 8; row++)
     {
          cout << endl;
          for (int column = 0; column <= 8; column++)
          {
              cout << Board[column][row];  // prints horizontally. 
          }
     }
}

int main()
{
    int Board[2][3];
    Display(Board);
}

thank you so much for the help AD. The references pass great. Just got to make the program work every time now.

> I have never in over 20 years seen anyone code a function prototype like that.
Edward has, but everyone has different experiences so there's no point arguing about what we've seen. :)

> Arrays are ALWAYS passed by reference, it is not possible to pass an array by value.
Arrays are passed by pointer, by default. With 1D arrays that's not a problem, but with 2D arrays it's confusing because people want to equate void foo(int array[2][3]) with void foo(int **array) , and that's not how it works. Saying they're passed by reference just adds to the confusion. :(

The difference between the default and Ed's example is with Ed's example the array really is passed by reference and you can use sizeof to get the size of the array rather than the size of a pointer to an array:

#include <iostream>

void ByRef(int (&array)[2][3])
{
  std::cout << "ByRef: " << sizeof array << '\n';
}

void ByPtr(int array[2][3])
{
  std::cout << "ByPtr: " << sizeof array << '\n';
}

int main()
{
  int array[2][3];

  ByRef(array);
  ByPtr(array);
}
commented: You are right! :) +30

Ah Ha! The reason I did not recognize that construct is because it is a C++ thingy, not C. I come from C world. I tried to compile it as a C program and got lots of errors.

Learned something new today, and that's good :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.