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?
To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.
A short answer is "no".
from array import * # import all package related to the array. arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements. print(arr1) # print the arr1 elements. # Use the insert() function to insert the element that contains two parameters.
There are three ways to pass a 2D array to a function:
The parameter is a 2D array
int array[10][10]; void passFunc(int a[][10]) { // ... } passFunc(array);
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With