dimanche 9 janvier 2022

Passing a 2D array as Function Argument

I am trying to pass 2D arrays of arbitrary size to a function. The code that i have tried is as follows:


#include <iostream>
void func(int (&arr)[5][6])
{
    std::cout<<"func called"<<std::endl;
}
int main()
{
    int arr[5][6];
    func(arr);
    return 0;
}

As you can see the func is correctly called. But i want to pass a 2D array of any size. In the current example, we can only pass int [5][6].

PS: I know i can also use vector but i want to know if there is a way to do this with array. For example, i should be able to write:

int arr2[10][15];
func(arr2);//this should work 

Aucun commentaire:

Enregistrer un commentaire