簡體   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