[英]2D array adding extra character
這是我的代碼,用於附加到2D數組並將其逐行顯示到網格中。
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");
}
}
基於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]
(二維數組中的第十個元素)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.