C++

Asked • 07/19/19

Passing a 2D array to a C++ function?

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){ myArray[x][y] = 5; etc... } And I have declared an array elsewhere in my code: double anArray[10][10]; However, calling `myFunction(anArray)` gives me an error. I do not want to copy the array when I pass it in. Any changes made in `myFunction` should alter the state of `anArray`. If I understand correctly, I only want to pass in as an argument a pointer to a 2D array. The function needs to accept arrays of different sizes also. So for example, `[10][10]` and `[5][5]`. How can I do this?

1 Expert Answer

By:

Patrick B. answered • 03/11/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.