C 2d数组结构给出了分段错误
这个程序片段应该读取以2d方式存储的结构中的信息。 但是这个程序片段在“printf(“lel4\n”);”之后给出了一个分段错误。 我感觉这是在我的记忆分配中,但我不知道。我在互联网上寻找答案,我正在做与给定答案完全相同的事情,但它不起作用C 2d数组结构给出了分段错误,c,pointers,struct,2d,C,Pointers,Struct,2d,这个程序片段应该读取以2d方式存储的结构中的信息。 但是这个程序片段在“printf(“lel4\n”);”之后给出了一个分段错误。 我感觉这是在我的记忆分配中,但我不知道。我在互联网上寻找答案,我正在做与给定答案完全相同的事情,但它不起作用 typedef struct coordinates{ int xposition; int ypositiony; char
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;
}
typedef结构坐标{
int-xposition;
内部位置;
字符标记;
int被发现;
}坐标
类型定义坐标*坐标;
坐标**allocMemory(整数行,整数列){
int i;
合作社**城市=马洛克(规模(合作社)*col);
断言(城市!=NULL);
对于(i=0;i
到
coord*
及
代码中没有2D数组,也没有可用作数组的构造。那么**城市在做什么?coord**
-->coord*
没有冒犯(老实说!),但是:正如你不得不问的那样,这意味着你不知道自己在做什么。我怀疑你只是遵循了某种模式。coord**
是指向指针的指针。如果指针与数组相同,它的调用也不会有什么不同。这里已经有几十个关于如何构造N-D数组的问题。请阅读一些内容。尝试重写代码thouttypedef坐标*coord;
coord **
city[i][j].xposition = i;
city[i][j].yposition = j;
city[i][j].token = c;
city[i][j].discovered = 0;