Reputation: 47
Can someone help me figure out how to pass a dynamically created array as function argument.
I've created a 2D dynamic array as follows:
//matrix rows and columns
int rows=0;
int cols=0;
int** matrix = new int*[rows];
//creates the matrix
for (int i =0; i < rows; ++i)
matrix[i] = new int[cols];
I'd like to pass this array into a function with a prototype like this:
void readMatrix(int **matrix[], int size);
I can't figure out how to do this! I don't know what the argument should look like, I've tried many different ways and none work. Getting errors that say "expecting expression" or "argument doesn't match parameter"
Upvotes: 0
Views: 81
Reputation: 5856
The function specifies the int **matrix[]
for the type, which is equivalent to a type int ***matrix
. So what you need is another indirection. Use &matrix
as an argument.
Also, as mentioned in comments, re-think the whole idea of using the plain arrays in favor of a more modern std::vector<>
one.
Upvotes: 2