2016-03-21 56 views
0

該程序片段應該讀取以二維方式存儲的結構信息。 但此程序片段在「printf(」lel4 \ n「);」之後給出了分段錯誤。 我感覺這是在我的記憶分配,但我不知道。我在網上尋找答案,我正在做與給定答案完全一樣的東西,但它不起作用。二維數組struct給出了分段錯誤

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; 
} 
+0

有一個在代碼沒有2D陣列和沒有構建體,其可以作爲一個。 – Olaf

+0

**城市在做什麼? – LifeisHard

+1

'coord **' - >'coord *' – BLUEPIXY

回答

0
coord ** 

coord *

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