# Thread: Returning a double matrix

1. ## Returning a double matrix

Hi there,

Do you know how to make **myMatrix a double pointer? This is what I have done considering it an int pointer...

Code:
```int **Matrix(int (*matrixSize))
{
(*matrixSize)=3; // number of rows.
(*matrixSize)=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.

// Allocating myMatrix (a two-dimensional array)...
int **myMatrix;

myMatrix=malloc((*matrixSize)*sizeof(int*));

if (myMatrix==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}

for (int i=0; i<(*matrixSize); i++)
{
myMatrix[i]=malloc((*matrixSize)*sizeof(int));

if (myMatrix==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}
}

// Filling myMatrix with numbers...
for (int i=0; i<(*matrixSize); i++)
for (int j=0; j<(*matrixSize); j++)
myMatrix[i][j]=5*i-2*j;

return &(myMatrix);
}

int main (int argc, const char * argv[])
{
int msize;

int **m=Matrix(&msize);

for (int i=0; i<msize; i++)
for (int j=0; j<msize; j++)
printf("m[%i][%i] = %i\n",i,j,m[i][j]);

return 0;
}```
Thank you very much for your kind of help.

Ricardo. 2. What have you tried? Does your int function work properly?

Jim 3. Code:
```int **Matrix(int (*matrixSize))
{
(*matrixSize)=3; // number of rows.
(*matrixSize)=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);
}```
Just do:
Code:
`return myMatrix;`
This:
Code:
```    for (int i=0; i<(*matrixSize); i++)
{
myMatrix[i]=malloc((*matrixSize)*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. 4. Dear quzah. Do you mean this?

Code:
```int **Matrix(int (*matrixSize));
int **Matrix(int (*matrixSize))
{
int numRows=3; // number of rows.
int numCols=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.

(*matrixSize)=numRows;
(*matrixSize)=numCols;

// Allocating myMatrix (a two-dimensional array)...
int **myMatrix;

myMatrix=malloc(numRows*sizeof(int*));

if (myMatrix==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}

for (int i=0; i<numRows; i++)
{
myMatrix[i]=malloc(numCols*sizeof(int));

if (myMatrix[i]==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}
}

// Filling myMatrix with numbers...
for (int i=0; i<numRows; i++)
for (int j=0; j<numCols; j++)
myMatrix[i][j]=5*i-2*j;

return myMatrix;
}

int main (int argc, const char * argv[])
{
//@autoreleasepool {
int msize;

int **m=Matrix(&msize);

for (int i=0; i<msize; i++)
for (int j=0; j<msize; j++)
printf("m[%i][%i] = %i\n",i,j,m[i][j]);
//}

return 0;
}```
What about making **myMatrix a double pointer?

Thank you! 5. Ok... I did it! Thank you Quzah. 6. Another question... to free memory, I need to do this, isn't it?

Code:
```for (int i=0; i<msize; i++)
free(m[i]);

free(m);```
I mean,

Code:
```#import <Foundation/Foundation.h>

double **Matrix(int (*matrixSize));
double **Matrix(int (*matrixSize))
{
int numRows=3; // number of rows.
int numCols=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.

(*matrixSize)=numRows;
(*matrixSize)=numCols;

// Allocating myMatrix (a two-dimensional array)...
double **myMatrix;

myMatrix=malloc(numRows*sizeof(double*));

if (myMatrix==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}

for (int i=0; i<numRows; i++)
{
myMatrix[i]=malloc(numCols*sizeof(double));

if (myMatrix[i]==NULL)
{
fprintf(stderr, "Error: Out of memory\n");
exit(-1);
}
}

// Filling myMatrix with numbers...
for (int i=0; i<numRows; i++)
for (int j=0; j<numCols; j++)
myMatrix[i][j]=1/9.*i-2.*j;

return myMatrix;
}

int main (int argc, const char * argv[])
{
@autoreleasepool {
int msize;

double **m=Matrix(&msize);

for (int i=0; i<msize; i++)
for (int j=0; j<msize; j++)
printf("m[%i][%i] = %f\n",i,j,m[i][j]);

// Realeasing memory...
for (int i=0; i<msize; i++)
free(m[i]);

free(m);
}

return 0;
}```

Thank you! Popular pages Recent additions 