I'm trying to use pointers of arrays to use as arguments for a function which generates an array.
void generateArray(int *a[], int *si){
srand(time(0));
for (int j=0;j<*si;j++)
*a[j]=(0+rand()%9);
} //end generateArray;
int main() {
const int size=5;
int a[size];
generateArray(&a, &size);
return 0;
} //end main
But when I compile this this message appears:
cannot convert `int (*)[5]' to `int**' for argument `1' to `void generateArray(int**, int*)'
generateArray(a, sizeof(a)/sizeof(a[0]))
. Verbose but this is standard best practice when working with arrays.std::vector
orstd::array
: They still know their size when passed to a function.std::vector<int>&
orstd::array<int>&
as the argument (or more likely, definegenerateArray
as a templated function and useT&
as the argument type); if you just do a straight swap fromint a[]
tostd::array<int, 5>
(or templatedT
used withstd::array<int, 5>
), you'll pass by value (copying stuff you didn't want to copy, operating on the copy, and leavinga
inmain
unmodified).