how to pass 2d array to a function as an argument
Passing 2d array to a function
@Rishabh-sahai
There are 3 ways to pass 2D array to a function:
- you need to specify the no of columns in each row
void passFunc(int a[][10])
{
// …
}
in main, you’ll write:
passFunc(array);
- passing as an array of pointers, here also you need to specify the no of columns in each row
void passFunc(int *a[10])
{
// …
}
in main function you call it like:
int *array[10];
for(int i = 0; i < 10; i++)
array[i] = new int[10];
passFunc(array);
- You can pass a 2D array as a pointer to a pointer:
void passFunc(int **a)
{
// …
}
inside main function, you can call the function like:
int **array;
array = new int *[10];
for(int i = 0; i <10; i++)
array[i] = new int[10];
passFunc(array);