gpt4 book ai didi

c - 二维数组添加额外的字符

转载 作者:太空宇宙 更新时间:2023-11-04 04:20:14 25 4
gpt4 key购买 nike

这是我的代码,用于附加到二维数组并将其逐行显示到网格中。

void map_print() {

char map[head->xdim][head->ydim]; // Generate 2D array for map
memset( map, 0, sizeof(map));

FEATURE *temp=head->next; // Get the pointer of the head of linked list

while(temp!=NULL) // Generated the map with the features
{
for (int xdim = 0; xdim < temp->xdim; xdim++){
map[temp->yloc][temp->xloc + xdim] = temp->type;
printf("X axis: Appeding to map[%d][%d]\n",temp->yloc,temp->xloc+xdim);
}
for (int ydim = 0; ydim < temp->ydim; ydim++){
map[temp->yloc + ydim][temp->xloc] = temp->type;
printf("Y axis: Appeding to map[%d][%d]\n",temp->yloc + ydim,temp->xloc);

}
temp=temp->next;
}

for (int i = 0; i < head->ydim; i++) { // Print out the map
for (int j = 0; j < head->xdim; j++) {
//printf("%c ", map[i][j]);
printf("map[%d][%d](%c)",i,j,map[i][j]);
}
printf("\n");
}
}

enter image description here

基于 printf,它应该只附加到以下坐标。但是,map(1)(4)、map(2)(4)、map(3)(4)、map(4)(4) 正在打印我没有附加到它的 *。

我找不到任何添加该额外字符的代码行

最佳答案

你混合了 x 和 y。声明是char map[head->xdim][head->ydim]; ([x][y]),但你像一样使用它map[temp->yloc][temp->xloc + xdim] = temp->type; ([y][x]).

如果您的数组大小为 [10][5] 并且您正在访问 [0][9] 它将调用未定义的行为(因为越界访问) 并且一种可能性是它将访问 [1][4](二维数组中的第十个元素)。

关于c - 二维数组添加额外的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com