C 段错误 : fscanf in a function

标签 c function matrix malloc scanf

我正在读取尺寸为 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/

相关文章:

postgresql - 如何将记录传递给 PL/pgSQL 函数?

python - 从协方差矩阵中查找高斯拟合误差

c - 在 C 中,如何将结构的成员声明为 volatile?

毕达哥拉斯三元组的 C 程序 - 输出不正确

c - ine 10 中 ';' 标记之前应为 '{'

function - 加入PySpark时有right_anti吗?

c - 下面的C程序中_AX = 1000是什么意思?

c# - Make efficient - 在 c# 中使用两个向量的对称矩阵乘法

c++ - 无法写入矩阵 <vector<vector<double>> 对象

C 连续二维数组指针运算的泛型函数