Reputation: 12797
I learned C programming language three years back and now when i am revisiting it after the experience of java and c# i'm facing some issues with pointers. So i tried to write a simple matrix addition program but i don't know why i'm getting some strange values while printing the matrices.
Code:
#include <stdio.h>
int* sumOfMat(int* m1,int* m2)
{
printf("Matrix A: \n");
printMat(m1);
printf("Matrix B: \n");
printMat(m2);
int mat3[3][3];
int row=0,col=0,k=0,sum=0;
for(;row<3;row++)
{
col=0;
for (;col<3 ;col++ )
{
sum=(*m1+*m2);
m1++;
m2++;
mat3[row][col]=sum;
}
}
printf("Result: \n");
// printMat(mat3); //this statement is giving me a correct output.
return mat3;
}
void printMat(const int m[3][3])
{
int row,col;
for (row=0;row<3 ;row++ )
{
for (col=0;col<3 ;col++ )
{
printf("%d\t",m[row][col]);
}
printf("\n");
}
}
int main(void) {
int mat1[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3]={{1,2,3},{4,5,6},{7,8,9}};
//add
printf("Sum of the metrices : \n");
int* x=sumOfMat(&mat1,&mat2);
printMat(x); // this call is providing me some garbage values at some locations.
return 0;
}
Output:
Success time: 0 memory: 2292 signal:0
Sum of the metrices :
Matrix A:
1 2 3
4 5 6
7 8 9
Matrix B:
1 2 3
4 5 6
7 8 9
Result:
2 134514448 134514448
8 10 12
14 16 -1216458764
Question: Why i'm getting this error and how to correct it.
Upvotes: 0
Views: 1668
Reputation: 106032
Never return a pointer to an automatic variable:
int *f(void)
{
int i;
....
return &i;
}
The variable i
doesn't exist once f
returns, so the pointer to it will be invalid.
In your case mat3
is an automatic variable and sumOfMat()
returns a pointer to mat3
which does't exist once sumOfMat()
returns.
One of the solution for your problem is, define mat3
as global variable.
Further I pointed out one major problem with your code:
Return type ofsumOfMat(int* m1,int* m2)
is pointer to integer (int *
) while you are returning mat3
which is of type int(*)[3]
.
I got lots of warnings while compiling your code. I corrected most of them.
Modified code:
#include <stdio.h>
void printMat(int *m);
int* sumOfMat(int* m1,int* m2);
int mat3[3][3];
int* sumOfMat(int* m1,int* m2)
{
printf("Matrix A: \n");
printMat(m1);
printf("Matrix B: \n");
printMat(m2);
//int mat3[3][3];
int row=0,col=0,sum=0;
for(;row<3;row++)
{
col=0;
for (;col<3 ;col++ )
{
sum=(*m1+*m2);
m1++;
m2++;
mat3[row][col]=sum;
}
}
printf("Result: \n");
// printMat(mat3); //this statement is giving me a correct output.
return mat3[0];
}
void printMat(int *m)
{
int row;
for (row=1;row<=9 ;row++)
{
printf("%d\t",*m++);
if(row%3 == 0)
printf("\n");
}
/* {
for (col=0;col<3 ;col++ )
{
printf("%d\t",m[row][col]);
}
printf("\n");
}*/
}
int main(void) {
int mat1[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[3][3]={{1,2,3},{4,5,6},{7,8,9}};
//add
printf("Sum of the metrices : \n");
int* x=sumOfMat(&mat1[0][0],&mat2[0][0]);
printMat(x); // this call is providing me some garbage values at some locations.
return 0;
}
Upvotes: 2
Reputation: 3520
It is because you are returning mat3
which is a local variable defined as
int mat3[3][3];
To correct this either do a dynamic allocation of mat3
using malloc
OR pass it as a out variable
in the function sumOfMat
as third variable.
Upvotes: 0
Reputation: 8126
the line
int mat3[3][3];
allocates your 2d array on the stack
You're returning a pointer to this array.
return mat3;
Unfortunately when the function call ends the stack is unwound and the memory for the array is no longer there, so you have a pointer to garbage.
One solution is to allocate the array in your main
function and pass it into sumOfMat
as a parameter.
Upvotes: 5