【问题标题】:C Segmentation fault: fscanf in a functionC 分段错误:函数中的 fscanf
【发布时间】: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", &amp;((*M)[l][c]));
  • 节省时间,启用所有应该警告fscanf(image, "%d", (*M)[l][c]);的编译器警告

标签: c function matrix malloc scanf


【解决方案1】:

        fscanf(image, "%d", (*M)[l][c]);

尝试一下

        fscanf(image, "%d", &((*M)[l][c]));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 2017-03-29
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多