Code:
int **Matrix(int (*matrixSize)[2])
{
(*matrixSize)[0]=3; // number of rows.
(*matrixSize)[1]=5; // number of cols.
// These numbers are obtained from another function.
// Not shown here, for the sake of simplicity.
// They are supposed to be 3 and 5, respectively.
Why don't you save yourself the trouble and just pass two integers, or an array of two integers? Why are you passing a pointer to an array of two integers?
Code:
// Allocating myMatrix (a two-dimensional array)...
int **myMatrix;
...
return &(myMatrix[0]);
}
Just do:This:
Code:
for (int i=0; i<(*matrixSize)[0]; i++)
{
myMatrix[i]=malloc((*matrixSize)[1]*sizeof(int));
if (myMatrix==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}
}
Should actually be:
Code:
if( myMagrix[ i ] == NULL )
...
You want to check the row you've allocated, not the base pointer. The base pointer will never be NULL here.
Quzah.