在我的代码中,我只是尝试使用函数Print_Matrix(M)打印初始化的矩阵,但当调用该函数时,我遇到了一个段错误,但当我在主函数内打印它时,它按预期打印出来。
这是复制问题的代码:
#include<stdio.h>
#include<stdlib.h>
int N = 5;
typedef struct matrix{
double m[1024][1024];
int size;
}matrix;
matrix I;
void
Print_Matrix(matrix M)
{
printf("hello\n");
int row=0, col=0;
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++){
printf(" %5.2f", M.m[row][col]);
}
printf("\n");
}
printf("\n\n");
}
int main()
{
int row, col;
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++) {
if (row == col)
I.m[row][col] = 1.0;
}
}
for(row=0;row<N;row++){
for(col=0;col<N;col++){
printf("%5.2f ", I.m[row][col]);
}
printf("\n");
}
Print_Matrix(I);
return 0;
}
输出:
1.00 0.00 0.00 0.00 0.00
0.00 1.00 0.00 0.00 0.00
0.00 0.00 1.00 0.00 0.00
0.00 0.00 0.00 1.00 0.00
0.00 0.00 0.00 0.00 1.00
Segmentation fault (core dumped)
N
是什么?它与MAX_SIZE
有什么关系?而MAX_SIZE
又是什么? - Some programmer dude