使用C,在初始化结构数组时,它看起来像是第一列,而最后一列是重叠的

使用C,在初始化结构数组时,它看起来像是第一列,而最后一列是重叠的,c,arrays,struct,C,Arrays,Struct,当主运行时,我得到了这个输出。第一列不知何故与最后一列混淆,即使它是专门初始化的 0 0 0 0 4 3 13 3 3 0 0 0 0 0 0 0 0 0 0 0 4 0 0 5 0 0 0 2 0 0 0 0 0 0 0 0 0 4 0 0 0 5 0 0 0 0 0 2 0 0 0 0 0 0 18 0 0 0 0 5 0 0 0 0 17 0 0 0 0 4 0 2 0 7 12 19 12 3 0 8 0 2 0 0 04000020 0 0 0 5 0 0 5 0 0 0 0 0 2

当主运行时,我得到了这个输出。第一列不知何故与最后一列混淆,即使它是专门初始化的

0 0 0 0 4 3 13 3 3 0 0 0 0 0 0
0 0 0 0 0 4 0 0 5 0 0 0 2 0 0 0 0 0
0 0 0 0 4 0 0 0 5 0 0 0 0 0 2 0 0 0 0
0 0 18 0 0 0 0 5 0 0 0 0 17 0 0
0 0 4 0 2 0 7 12 19 12 3 0 8 0 2 0 0
04000020
0 0 0 5 0 0 5 0 0 0 0 0 2
2 0 0 11 0 0 5 0 0 11 0 0 1
17772071477207771
10011000500110001
100 0 0 1 0 0 5 0 0 0 1 0 0 0
06000500080
0 0 6 0 4 0 7 12 19 12 3 0 6 0 8 0 0
0 0 0 15 0 0 0 0 5 0 0 0 0 16 0 0 0
0 0 0 6 0 0 0 5 0 0 0 8 0 0 0 0
0 0 0 0 6 0 5 0 0 8 0 0 0 0 0 0
0 0 0 0 7 7 7 8 0 0 0 0 0 0 0 0

动议.h

struct square{
  int directions;
  int isRobotHere;
  int isMultipleDirections;
  int printable;
};
typedef struct square Square;

struct robot{
  int robotx;
  int roboty;
  int robotz;
  int destinationx;
  int destinationy;
  int destinationz;
};
typedef struct robot Robot;
enum direction{North, NorthWest, West, SouthWest, South, SouthEast, East, NorthEast};
Square firstfloor[16][16];
Square secondfloor[16][16];
void printbothfloors();
void initializeArrays();
缩写为arrays.c

#include <stdio.h>
#include "motion.h"
void initializeArrays(){
firstfloor[6][0].directions=5;
firstfloor[7][0].directions=5;
firstfloor[8][0].directions=14;
firstfloor[9][0].directions=5;
firstfloor[10][0].directions=6;
firstfloor[11][1].directions=6;
firstfloor[12][2].directions=6;
firstfloor[13][3].directions=15;
firstfloor[14][4].directions=6;
firstfloor[15][5].directions=6;
firstfloor[16][6].directions=7;
firstfloor[16][7].directions=7;
firstfloor[16][8].directions=7;
firstfloor[16][9].directions=7;
firstfloor[16][10].directions=8;
firstfloor[15][11].directions=8;
firstfloor[14][12].directions=8;
firstfloor[13][13].directions=16;
firstfloor[12][14].directions=8;
firstfloor[11][15].directions=8;
firstfloor[10][16].directions=1;
firstfloor[9][16].directions=1;
firstfloor[8][16].directions=1;
firstfloor[7][16].directions=1;
firstfloor[6][16].directions=2;
firstfloor[5][15].directions=2;
firstfloor[4][14].directions=2;
firstfloor[3][13].directions=17;
firstfloor[2][12].directions=2;
firstfloor[1][11].directions=2;
firstfloor[0][10].directions=3;
firstfloor[0][9].directions=3;
firstfloor[0][8].directions=13;
firstfloor[0][7].directions=3;
firstfloor[0][6].directions=4;
firstfloor[1][5].directions=4;
firstfloor[2][4].directions=4;
firstfloor[3][3].directions=18;
firstfloor[4][2].directions=4;
firstfloor[5][1].directions=4;
firstfloor[1][8].directions=5;
firstfloor[2][8].directions=5;
firstfloor[3][8].directions=5;
firstfloor[4][8].directions=19;
firstfloor[5][8].directions=5;
firstfloor[6][8].directions=5;
firstfloor[7][8].directions=5;
firstfloor[8][8].directions=14;
firstfloor[9][8].directions=5;
firstfloor[10][8].directions=5;
firstfloor[11][8].directions=5;
firstfloor[12][8].directions=19;
firstfloor[13][8].directions=5;
firstfloor[14][8].directions=5;
firstfloor[15][8].directions=5;
firstfloor[8][1].directions=7;
firstfloor[8][2].directions=7;
firstfloor[8][3].directions=7;
firstfloor[8][4].directions=20;
firstfloor[8][5].directions=7;
firstfloor[8][6].directions=7;
firstfloor[8][7].directions=7;
firstfloor[8][9].directions=7;
firstfloor[8][10].directions=7;
firstfloor[8][11].directions=7;
firstfloor[8][12].directions=20;
firstfloor[8][13].directions=7;
firstfloor[8][14].directions=7;
firstfloor[8][15].directions=7;
firstfloor[7][4].directions=11;
firstfloor[9][4].directions=11;
firstfloor[4][7].directions=12;
firstfloor[4][9].directions=12;
firstfloor[12][7].directions=12;
firstfloor[12][9].directions=12;
firstfloor[7][12].directions=11;
firstfloor[9][12].directions=11;
firstfloor[4][4].directions=2;
firstfloor[4][6].directions=7;
firstfloor[4][10].directions=3;
firstfloor[4][12].directions=8;
firstfloor[6][4].directions=5;
firstfloor[6][12].directions=5;  
 firstfloor[10][4].directions=1;
firstfloor[10][12].directions=1;
firstfloor[12][4].directions=4;
firstfloor[12][6].directions=7;
firstfloor[12][10].directions=3;
firstfloor[12][12].directions=6;
firstfloor[11][0].directions=0;
firstfloor[5][16].directions=0;
}
#包括
#包括“motion.h”
void initializeArrays(){
一楼[6][0],方向=5;
一楼[7][0],方向=5;
一楼[8][0],方向=14;
一楼[9][0],方向=5;
一楼[10][0],方向=6;
一楼[11][1],方向=6;
一楼[12][2],方向=6;
一楼[13][3],方向=15;
一楼[14][4],方向=6;
一楼[15][5],方向=6;
一楼[16][6],方向=7;
一楼[16][7]。方向=7;
一楼[16][8],方向=7;
一楼[16][9]。方向=7;
一楼[16][10]。方向=8;
一楼[15][11]。方向=8;
一楼[14][12]。方向=8;
一楼[13][13]。方向=16;
一楼[12][14]。方向=8;
一楼[11][15]。方向=8;
一楼[10][16],方向=1;
一楼[9][16],方向=1;
一楼[8][16],方向=1;
一楼[7][16],方向=1;
一楼[6][16],方向=2;
一楼[5][15],方向=2;
一楼[4][14],方向=2;
一楼[3][13],方向=17;
一楼[2][12],方向=2;
一楼[1][11],方向=2;
一楼[0][10],方向=3;
一楼[0][9],方向=3;
一楼[0][8],方向=13;
一楼[0][7],方向=3;
一楼[0][6],方向=4;
一楼[1][5],方向=4;
一楼[2][4],方向=4;
一楼[3][3],方向=18;
一楼[4][2],方向=4;
一楼[5][1],方向=4;
一楼[1][8],方向=5;
一楼[2][8],方向=5;
一楼[3][8],方向=5;
一楼[4][8],方向=19;
一楼[5][8],方向=5;
一楼[6][8],方向=5;
一楼[7][8],方向=5;
一楼[8][8],方向=14;
一楼[9][8],方向=5;
一楼[10][8],方向=5;
一楼[11][8],方向=5;
一楼[12][8]。方向=19;
一楼[13][8],方向=5;
一楼[14][8],方向=5;
一楼[15][8],方向=5;
一楼[8][1],方向=7;
一楼[8][2],方向=7;
一楼[8][3],方向=7;
一楼[8][4],方向=20;
一楼[8][5],方向=7;
一楼[8][6],方向=7;
一楼[8][7],方向=7;
一楼[8][9],方向=7;
一楼[8][10]。方向=7;
一楼[8][11],方向=7;
一楼[8][12]。方向=20;
一楼[8][13]。方向=7;
一楼[8][14]。方向=7;
一楼[8][15]。方向=7;
一楼[7][4],方向=11;
一楼[9][4],方向=11;
一楼[4][7],方向=12;
一楼[4][9],方向=12;
一楼[12][7],方向=12;
一楼[12][9]。方向=12;
一楼[7][12]。方向=11;
一楼[9][12]。方向=11;
一楼[4][4],方向=2;
一楼[4][6],方向=7;
一楼[4][10],方向=3;
一楼[4][12]。方向=8;
一楼[6][4],方向=5;
一楼[6][12],方向=5;
一楼[10][4],方向=1;
一楼[10][12],方向=1;
一楼[12][4],方向=4;
一楼[12][6],方向=7;
一楼[12][10]。方向=3;
一楼[12][12],方向=6;
一楼[11][0],方向=0;
一楼[5][16],方向=0;
}
打印两层楼

#include <stdio.h>
#include <stdlib.h>
#include "motion.h"
void printbothfloors(){
  //  printf("printfloor is running");
  int upper, lower, i, j;
  printf("%4d %4d %4d %4d %4d", firstfloor[6][0].directions, firstfloor[7][0].directions, firstfloor[8][0].directions, firstfloor[9][0].directions, firstfloor[10][0].directions);
  printf("%4d %4d %4d %4d %4d", firstfloor[6][16].directions, firstfloor[7][16].directions, firstfloor[8][16].directions, firstfloor[9][16].directions, firstfloor[10][16].directions);


  printf("FIRST FLOOR");
  printf("\n-");
  /* The next for loop prints out the upper edge */
  for (upper = 0; upper < 18; upper++){
    printf("----");
  }
  printf("\n");

  /*The next for loop prints out the floor, every element is 4 digits wide */
  for (i = 0; i <= 16; i++){
    printf("|");
    for(j = 0; j <= 16; j++){
      printf("%4d", firstfloor[i][j].directions);
    }
    printf("   |\n");
  }
  /* The next for loop prints out the lower edge */
  for (lower = 0; lower < 18; lower++){
    printf("----");
  }
  printf("-\n");
  /*
  printf("SECOND FLOOR");
  //  printf("printfloor is running");
  printf("\n-----");
  // The next for loop prints out the upper edge 
  for (upper = 0; upper < 17; upper++){
    printf("----");
  }
  printf("\n");

  //The next for loop prints out the floor, every element is 4 digits wide 
  for (i = 0; i <= 16; i++){
    printf("|");
    for(j = 0; j <= 16; j++){
      printf("%4d", secondfloor[i][j].directions);
    }
    printf("   |\n");
  }
  // The next for loop prints out the lower edge 
  for (lower = 0; lower < 17; lower++){
    printf("----");
  }
  printf("-----\n");
*/
}
#包括
#包括
#包括“motion.h”
void printfoors(){
//printf(“printfloor正在运行”);
int上,下,i,j;
printf(“%4d%4d%4d%4d%4d%4d”,一楼[6][0]。方向,一楼[7][0]。方向,一楼[8][0]。方向,一楼[9][0]。方向,一楼[10][0]。方向);
printf(“%4d%4d%4d%4d%4d%4d”,一楼[6][16]。方向,一楼[7][16]。方向,一楼[8][16]。方向,一楼[9][16]。方向,一楼[10][16]。方向);
printf(“一楼”);
printf(“\n-”);
/*下一个for循环打印出上边缘*/
用于(上限=0;上限<18;上限++){
printf(“---”);
}
printf(“\n”);
/*下一个for循环打印出地板,每个元素都有4位数宽*/

对于(i=0;i类型arr[N]
的有效指数介于
0
N-1
之间


这适用于任何
类型和任何数量的维度。

类型arr[N]
的有效索引介于
0
N-1
之间


这适用于任何
类型和任何数量的维度。

C中的索引从0开始,因此如果您声明数组有16个元素,则有效索引从0开始,在15结束

16箱,编号为0至15:

---------------------------------------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
---------------------------------------------------------------------------------

访问此范围之外的元素(例如,通过使用索引-1或16)会调用未定义的行为。

C中的索引从0开始,因此如果您声明ar
---------------------------------------------------------------------------------
|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
---------------------------------------------------------------------------------
firstfloor[6][16].directions
/*The next for loop prints out the floor, every element is 4 digits wide */
for (i = 0; i <= 16; i++){
  printf("|");
  for(j = 0; j <= 16; j++){
    printf("%4d", firstfloor[i][j].directions);
  }
  printf("   |\n");
}