2016-03-21 55 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;