# Thread: simple matrix

1. ## simple matrix

Hey so I need to print out a matrix using malloc function. I have done that part and this is my code
Code:
```#include<stdio.h>
#include <stdlib.h>

int main (void)
{
int row_size1,col_size1,row_size2,col_size2;
int **A;
int i = 0, j = 0;

//allocate space for 1 matrix
A = malloc(1 * sizeof (int *));

printf("Enter the number of rows and columns:");
scanf("%d %d",&row_size1,&col_size1);

A= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}

printf("Enter the elements of the first matrix write 99 when done:");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
if (A[i][j] == 99) // 'x' is character variable I declared to \
break;
}
}

printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
free(A);
}```

So the catch is I don't want to ask the users seperately the number of rows and columns of a matrix. I want them to tell me at the same time they are telling me the matrix.
for example:
Code:
```printf("What is your matrix")

(user) 2 2 1 2 3 4```
so I want the code to record the first 2 values (2,2) as the number of rows and columns and then print out the rest of the matrix.
The only thing confusing me about this is how I am supposed to allocate space for a matrix I don't know the dimensions of

Thanks

2. Read the row and value from the user before allocating memory for the matrix. Just don't tell the user what you're doing.

3. There are a lot of memory leaks in your program.

First allocation of "A" gets disregarded and not freed -> It's not needed.

Don't cast malloc. Also, each element you allocate here needs to be freed individually.
Code:
`A[i]=(int*)malloc(col_size1*sizeof(int));`

4. Is this what you meant?
Its still not compiling
Code:
```#include<stdio.h>
#include <stdlib.h>

int main (void)
{
int row_size1,col_size1,row_size2,col_size2;
int **A;
int i = 0, j = 0;

//allocate space for 1 matrix
A = malloc(1 * sizeof (int *));

/* printf("Enter the number of rows and columns:");
scanf("%d %d",&row_size1,&col_size1);

A= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}
*/
printf("Enter the elements of the first matrix write 99 when done:");
row_size1= A[0][0];
col_size1= A[0][1];

A= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));

}

for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
if (A[i][j] == 99) // 'x' is character variable I declared to \
break;
}
}

printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
free(col_size1);
}```
Also could you please explain what you meant by not casting malloc

5. No.

You have kept the first allocation and commented out the second. As soon as the next malloc is called, the address of "A" is overwritten with another address -> The first address is lost and therefore can not be freed.

Uncomment what you have commented out and then comment the First ​malloc.

You are still casting malloc with (int *) - FAQ: Casting malloc?

6. Wait.. so the problem I have is if I uncomment what I have commented how will the program know what row_size1 and col_size1 are?
This is what I think you mean
Code:
```#include<stdio.h>
#include <stdlib.h>

int main (void)
{
int row_size1,col_size1,row_size2,col_size2;
int **A;
int i = 0, j = 0;

printf("Enter the elements of the first matrix write 99 when done:");
row_size1= A[0][0];
col_size1= A[0][1];

A= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=malloc(col_size1*sizeof(int));

}
//allocate space for 1 matrix
A = malloc(1 * sizeof (int *));

for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
if (A[i][j] == 99) // 'x' is character variable I declared to \
break;
}
}

printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
free(col_size1);
}```
p.s please bear with me I'm a bit slow but I really want to learn this stuff

7. Uncomment what you have commented out and then comment the Firstmalloc.
This is what I meant:
Code:
```  //allocate space for 1 matrix
//A = malloc(1 * sizeof (int *));

printf("Enter the number of rows and columns:");
scanf("%d %d",&row_size1,&col_size1);

A= malloc(row_size1*sizeof(int*));

for(i=0;i<row_size1;i++)
{
A[i]= malloc(col_size1*sizeof(int));
}```
And to free the allocated memory
Code:
```  for(i=0;i<row_size1;i++)
{
free(A[i]);
}

free(A);```

8. I dont want to seperately ask the user for the number of rows and columns. I want the program to run like this
Code:
```printf("Enter the elements of the first matrix write 99 when done:");
(user) 2 2 1 2 3 4

then I want 2 and 2 to be the number of rows and columns and 1 2 3 4 to be the 2 by 2 matrix printed
```

9. Originally Posted by zafy
I dont want to seperately ask the user for the number of rows and columns.
Then don't ask the user. Just read the row and column using scanf. You don't have to tell the user that you're reading the row and column before you allocate memory and run through the loop for reading each value in the matrix.

10. I was pointing out a memory leak in the code you had provided - If you want to not do it that way any more, that's ok.

11. Isnt that what I'm doing when I am writing this in my code
row_size1= A[0][0]; //first element is row
col_size1= A[0][1];

12. No. Read two integers into separate variables. The matrix can't be allocated until you know how big to make it, so you can't use A[0][0] and A[0][1] until you get the size from the user.

13. I was pointing out a memory leak in the code you had provided - If you want to not do it that way any more, that's ok.
Thank you for the memory leak. I kind of mentioned in the beginning that this was the trick in the program I wasn't able to do. Sorry I dodnt mean to confuse you

14. I dont get how to read 2 integers from 6 integers that the user has provided without using A[0][1].
Could you demonstrate it please

15. It looks like you already know how to read two integers from the user:
Code:
`scanf("%d %d",&row_size1,&col_size1);`
(I took this line from your post from an hour ago)

Popular pages Recent additions