繁体   English   中英

2D数组增加了额外的字符

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM