2017-11-28 6 views
1

Dies ist mein Code zum Anhängen an das 2D-Array und zum Anzeigen von Zeile für Zeile in einem Raster.2D-Array fügt zusätzliches Zeichen hinzu

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

Basierend auf dem printf, sollte es nur auf die folgenden Koordinaten anhängen. Die Karte (1) (4), die Karte (2) (4), die Karte (3) (4), die Karte (4) (4) drucken jedoch *, die ich nicht angehängt habe.

Ich kann nicht jede Linie meines Code finden, dass zusätzliches Zeichen hinzugefügt wird

Antwort

0

Sie gemischt x und y. Die Deklaration ist char map[head->xdim][head->ydim]; ([x][y]), aber Sie verwenden es wie map[temp->yloc][temp->xloc + xdim] = temp->type; ([y][x]).

Wenn die Größe Ihres Arrays ist [10][5] und Sie zugreifen [0][9] es wäre nicht definiertes Verhalten aufrufen (wegen der außerhalb der Grenzen Zugang) und eine Möglichkeit ist, dass es [1][4] zugreifen würde (das zehnte Element im Array 2D) statt.

Verwandte Themen