我正在读取尺寸为 512*681 的 .pgm 文件。
我的调试器将段错误指向 fscanf(image, "%d", (*M)[i][j]);
。我该如何解决?
我没有添加一些函数,例如 open_pgm
,因为我确信它们一切正常。
输入
12
13
12
12
12
12
10
...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void Store(int ***M, int lines, int columns, FILE *image)
{
for(int l = 0; l < lines; l++)
{
for(int c = 0; c < columns; c++)
{
fscanf(image, "%d", (*M)[l][c]);
}
}
}
int main(int argc, char *argv[])
{
FILE *image;
char *string;
int lines;
int columns;
string = malloc(18*sizeof(char));
open_pgm(string, argv, &image);
fscanf(image, "%d %d", &lines, &columns);
int **M;
allocMatrix(&M, lines, columns);
Store(&M, lines, columns, image);
printf("%d\n", M_lida[0][0]);
close(&imagem);
close(&texto);
return 0;
}
最佳答案
对于
fscanf(image, "%d", (*M)[l][c]);
试试看
fscanf(image, "%d", &((*M)[l][c]));
关于C 段错误 : fscanf in a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072136/