# 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)[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.

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

myMatrix=malloc((*matrixSize)[0]*sizeof(int*));

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

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);
}
}

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

return &(myMatrix[0]);
}

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

int **m=Matrix(&msize);

for (int i=0; i<msize[0]; i++)
for (int j=0; j<msize[1]; 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)[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:
Code:
`return myMatrix;`
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.

4. Dear quzah. Do you mean this?

Code:
```int **Matrix(int (*matrixSize)[2]);
int **Matrix(int (*matrixSize)[2])
{
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)[0]=numRows;
(*matrixSize)[1]=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[2];

int **m=Matrix(&msize);

for (int i=0; i<msize[0]; i++)
for (int j=0; j<msize[1]; 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[0]; i++)
free(m[i]);

free(m);```
I mean,

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

double **Matrix(int (*matrixSize)[2]);
double **Matrix(int (*matrixSize)[2])
{
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)[0]=numRows;
(*matrixSize)[1]=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[2];

double **m=Matrix(&msize);

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

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

free(m);
}

return 0;
}```

Thank you!