我有一个简单的矩阵运算程序,但是我有一个问题->例如我有数组 [3] [3],我需要一些方法来获取维数-在这种情况下为 3,这是代码:
#include <stdio.h>
#include <stdlib.h>
int **count()
{
printf("Write number of rows and collumns in format ROWS space COLLUMNS");
int i = 0;
int j = 0;
scanf("%i %i", &i, &j);
int **mat1 = (int**)malloc(i*sizeof(int*));
for (int x = 0; x < j;x++){
mat1[x] = (int*)malloc(j*sizeof(int));
}
for (int x = 0; x < i;x++){
for (int y = 0; y < j;y++){
scanf("%i",&mat1[x][y]);
}
}
printf(Write number of rows and collumns in format ROWS space COLLUMNS");
int i2 = 0,j2 = 0;
scanf("%i %i", &i2, &j2);
int **mat2 = (int**)malloc(i2*sizeof(int*));
for (int x2 = 0; x2 < j2;x2++){
mat2[x2] = (int*)malloc(j2*sizeof(int));
}
for (int x2 = 0; x2 < i2;x2++){
for (int y2 = 0; y2 < j2;y2++){
scanf("%i",&mat2[x2][y2]);
}
}
int i3 = i, j3 = j;
int **mat3 = (int**)malloc(i3*sizeof(int*));
for (int x = 0; x < j3;x++){
mat3[x] = (int*)malloc(j3*sizeof(int));
}
for (int x3 = 0; x3 < i3;x3++){
for (int y3 = 0; y3 < j3;y3++){
mat3[x3][y3] = mat1[x3][y3] + mat2[x3][y3];
}
}
return mat3;
}
int writeMatrix(int **mat, int rows, int collumns)
{
int i = rows, j=collumns;
for (int x = 0; x < i;x++){
for (int y = 0; y < j;y++){
printf("%3i ",mat[x][y]);
}
printf("\n");
}
return 0;
}
int main()
{
int **m1 = count();
writeMatrix(m1,x,x);//HERE I NEED TO KNOW NUMBER OF ROWS AND COLLUMNS
free(m1);
}
这是在普通数组 [] [] 的情况下对我有用但在这种情况下不适用于我的代码 -
int y = (sizeof(m1)/sizeof(m1[0][0])) / (sizeof(m1)/sizeof(m1[0]));