有人可以帮我理解C中的结构错误吗?这是我的代码:
struct Orientation { char facing; char sensor; char mazeDir; }; struct Orientation O[16]; O[0] = {'N', 'F', 'N'}; O[1] = {'N', 'B', 'S'}; O[2] = {'N', 'R', 'E'}; O[3] = {'N', 'L', 'W'}; O[4] = {'S', 'F', 'S'}; O[5] = {'S', 'B', 'N'}; O[6] = {'S', 'R', 'W'}; O[7] = {'S', 'L', 'E'}; O[8] = {'E', 'F', 'E'}; O[9] = {'E', 'B', 'W'}; O[10] = {'E', 'R', 'S'}; O[11] = {'E', 'L', 'N'}; O[12] = {'W', 'F', 'W'}; O[13] = {'W', 'B', 'E'}; O[14] = {'W', 'R', 'N'}; O[15] = {'W', 'L', 'S'};
我得到关于O [13],O [14]和O [15]的这些错误
..\RMNCODE.C(282): error C231: 'O': redefinition ..\RMNCODE.C(283): error C279: 'O': multiple initialization ..\RMNCODE.C(283): error C231: 'O': redefinition ..\RMNCODE.C(284): error C279: 'O': multiple initialization ..\RMNCODE.C(284): error C231: 'O': redefinition
ralphtheninj.. 8
相反地初始化数组:
struct Orientation O[16] = { {'N', 'F', 'N'}, {'N', 'B', 'S'}, {'N', 'R', 'E'}, {'N', 'L', 'W'}, {'S', 'F', 'S'}, {'S', 'B', 'N'}, {'S', 'R', 'W'}, {'S', 'L', 'E'}, {'E', 'F', 'E'}, {'E', 'B', 'W'}, {'E', 'R', 'S'}, {'E', 'L', 'N'}, {'W', 'F', 'W'}, {'W', 'B', 'E'}, {'W', 'R', 'N'}, {'W', 'L', 'S'} };
干杯!
相反地初始化数组:
struct Orientation O[16] = { {'N', 'F', 'N'}, {'N', 'B', 'S'}, {'N', 'R', 'E'}, {'N', 'L', 'W'}, {'S', 'F', 'S'}, {'S', 'B', 'N'}, {'S', 'R', 'W'}, {'S', 'L', 'E'}, {'E', 'F', 'E'}, {'E', 'B', 'W'}, {'E', 'R', 'S'}, {'E', 'L', 'N'}, {'W', 'F', 'W'}, {'W', 'B', 'E'}, {'W', 'R', 'N'}, {'W', 'L', 'S'} };
干杯!
如果您要初始化O,则需要在声明中一次完成所有操作:
struct Orientation O[16] = { { 'N', 'F', 'N' }, { 'N', 'B', 'S'), ...
};
你不能这样做:
O[0] = {'N', 'F', 'N'};
因为C不支持语句中的结构文字,只作为初始化列表的一部分.