Please help me debug this little C program on dynamic two-dimensional array? [migrated]
- by azhi
I am a newbie here. I have written a little C program, which is to create a two-dimensional matrix. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int **CreatMatrix(int m,int n){
int **Matrix;
int i;
Matrix=(int**)malloc(m*sizeof(int*));
for(i=0;i<m;i++){
Matrix[i]=(int*)malloc(n*sizeof(int));
}
return Matrix;
}
int main(){
int m,n;
int **A;
printf("Please input the size of the Matrix: ");
scanf("%d%d",&m,&n);
A=CreatMatrix(m,n);
printf("Please input the entries of the Matrix, which should be integers!\n");
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&A[i][j]);
}
}
printf("The Matrix that you input is:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%3d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
free(A[i]);
free(A);
}
I have run it, and it works fine. But I am not sure if it is right? Can anyone help me debug it?