繁体   English   中英

2D数组结构给出分段错误

[英]2d array struct giving a segmentation fault

该程序片段应读取以二维方式存储的结构信息。 但是此程序片段在“ 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 ** 

coord *

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM