Passing an array by reference to a function

I'm running into some problems when I try to pass an array in a function by reference. I get an error saying I can't do that. I'm sure there is a workaround way to do it, but I'm running into problems.

I tried making a pointer to the array (thus the first element in the array) and passing the pointer by reference (function is a void), then reconstruct the array once the function finished, but that didn't work. I need some way to pass the address of the first element of the array.

Just so you know why I need this, I'm writing a program that will randomly generate a 12 by 12 maze. I need to be able to declare the char array in main, pass it into a void function, then print the char array in main. Global variables are out of the question (bad programming).

Thanks for any input!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arrays are always passed by its first location.
Simple example:

void foo(int* numbers, int size)
   for (int i = 0; i < 10; ++i)
      numbers[i] = i*2;

void main(int argc, char **argv)
   int numbers[10];
   foo(numbers, 10);
   // Print numbers, you will see its changed inside foo(...)
If you are passing 2-dimensional array then do something like this

void foo(int numbers[][2], int row, int col)
      for (int i = 0; i < row; ++i)
            for (int j = 0; j < col; ++j)
                  numbers[i][j] = i*2;

void main(int argc, char **argv)
      int numbers[3][2];
      foo(numbers, 3, 2);
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

You actually can pass an array by reference, but this is rarely done, because if you just pass the array as a parameter, it automatically "decays" to a pointer to the first element, and so the called function can operate on the original, not a copy, as in the comments previously posted.

The syntax for passing a reference to an array would look like this:

void func(char (&array)[59]);

For more details, see:
Like any other type, pointer can be passed by reference. In this case called function can read array contents, and also release and allocate this array:

int* array = new int[2];
cout << array[2];   // this is another array
delete[] array;

void function(int** array)
    // array contents is available here, like when array pointer is passed by value:
    cout << (*array)[0];

    // since array pointer is passed by reference, this function can make additional operations:
    if ( *array != NULL )
         delete[] array;

    *array = new int[3];

jrobinson3k1Author Commented:
When I use that efn, on this line:

randomMaze(maze2, start2);

I get this error message:

undefined reference to `randomMaze(char (&) [12][12], int (&) [2])'

Here is my header:

void randomMaze(char(&array)[12][12], int(&array)[2]);

maze2 is defined as a 12x12 char array and start2 is an int array of size 2.
First, you have two parameters declared with the same name, "array".  This is not a good idea--when you refer to "array" in the function body, how will the compiler know which one you mean?  I would expect your compiler to give you a warning about this.

But that doesn't match the error message you showed.  The error message suggests that the implementation of randomMaze did not match the declaration.  Did you change it in both places?

I tried variants of this snippet in two compilers and it compiled in both.

void randomMaze(char(&charray)[12][12], int(&array)[2])

int main(int argc, char *argv[])
      char ar[12][12];
      int intar[2];
      randomMaze(ar, intar);
      return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jrobinson3k1Author Commented:
Ahhh, I got confused on the syntax a bit...I was trying to give each array two variable names lol. I'm still new, but I'm getting there ;) Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.