I have a method that will take a 2D array of configurable size as a parameter.

So far, I've got:

```void myFunction(double** myArray){
myArray[x][y] = 5;
etc...
}
```

And I have declared an array elsewhere in my code:

```double anArray[10][10];
```

However, when I call myFunction(anArray), I get an error. I don't want to make a copy of the array when I send it in.

Any modifications to myFunction should have an effect on the state of anArray.

If I understand well, I simply want to send a pointer to a 2D array as a parameter.

The function must also take arrays of varying sizes. For instance, [10][10] and [5][5]. How can I accomplish this?

Aug 2, 2022 in C++ 27,436 views

## 1 answer to this question.

There are three ways to pass a 2D array to a function:

1. The parameter is a 2D array

```int array[10][10];
void passFunc(int a[][10])
{
// ...
}
passFunc(array);
```
2. The parameter is an array containing pointers

```int *array[10];
for(int i = 0; i < 10; i++)
array[i] = new int[10];
void passFunc(int *a[10]) //Array containing pointers
{
// ...
}
passFunc(array);
```
3. The parameter is a pointer to a pointer

```int **array;
array = new int *[10];
for(int i = 0; i <10; i++)
array[i] = new int[10];
void passFunc(int **a)
{
// ...
}
passFunc(array);```
answered Aug 2, 2022 by
• 4,960 points

## What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

## How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

## How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

## How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

## How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

## Simple C++ swap function

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses? It does not work. void num_exchange(int m, int n); int main(){ int num1 ...READ MORE

## How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

## How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE