c - 二维数组结构出现段错误

标签 c pointers struct 2d

该程序片段应该读取以二维方式存储的结构中的信息。 但是这个程序片段在“printf(”lel4\n”);”之后给出了段错误。 我感觉它在我的内存分配中,但我不知道。我在互联网上寻找答案,我正在做与给定答案完全相同的事情,但它不起作用。

typedef struct coordinates{ 
    int xposition;                          
    int ypositiony;                         
    char token;                     
    int discovered;                 
} coordinates

typedef coordinates *coord;

coord **allocMemory(int row, int col){
    int i;
    coord **city = malloc(sizeof(coord)*col);
    assert(city != NULL);
    for(i=0; i<col; i++){
        city[i] = malloc(sizeof(struct coordinates)*row);
        assert(city[i] != NULL);
    }
    return city;
}


coord **readInfo(int row, int col){
    int i, j;
    char c;
    coord **city = allocMemory(row, col);
    for(i=0; i<col;i++){
        c = getchar();
        for(j = 0; j<ros; j++){
            c = getchar();
            if(c == '#' || c == '.'){
                printf("lel4\n");
                (*city)[i][j].xposition = i;
                printf("lel5\n");
                (*city)[i][j].yposition = j;
                (*city)[i][j].token = c;
                (*city)[i][j].discovered = 0;
            }
        }
    }
    return city;
}

最佳答案

coord ** 

坐标*

city[i][j].xposition = i;
city[i][j].yposition = j;
city[i][j].token = c;
city[i][j].discovered = 0;

关于c - 二维数组结构出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36138830/

相关文章:

c - sqlite3的共享库中可能存在内存泄漏

c# - 带有 SOS 的 Windbg,如何转储 c# 结构

c++ - 检查结构数组是否为空

用作结构字段或结构方法

c - 升级到Mojave后,无法在Mac上编译C程序

c++ - 带有特殊字符的字符串的控制台输出

c - 段错误转换成二进制

c++ - 将文本转换为数组

c - 删除递归 void 函数

c++ - 传递给函数时如何使结构为空?