【发布时间】:2018-10-08 20:58:21
【问题描述】:
我正在读取尺寸为 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;
}
【问题讨论】:
-
假设
M被正确分配,你应该使用fscanf(image, "%d", &((*M)[l][c])); -
节省时间,启用所有应该警告
fscanf(image, "%d", (*M)[l][c]);的编译器警告
标签: c function matrix malloc scanf