简体   繁体   English

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

[英]2d array struct giving a segmentation fault

This program fragment should be reading in information in structs which are stored in a 2d way. 该程序片段应读取以二维方式存储的结构信息。 But this program fragment gives a segmentation fault after "printf("lel4\\n");". 但是此程序片段在“ printf(“ lel4 \\ n”);”之后给出了分段错误。 I have the feeling it is in my memory allocation, but I don't know. 我感觉它在我的内存分配中,但是我不知道。 I looked for answers on the internet and I am doing exactly the same thing as the given answers, but it isn't working. 我在互联网上寻找答案,并且正在做与给出的答案完全相同的事情,但是它没有用。

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 ** 

to coord * coord *

and

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