创建结构实例数组[C]

时间:2016-11-11 16:45:16

标签: c arrays struct

我正在为嵌入式系统实验室创建一个有限状态机。编译器(我正在使用ARM的Kiel)抱怨我试图声明一个struct的实例数组。我做错了什么?

我的代码(部分内容):

struct State {
    unsigned long OutCars;
    unsigned long OutWalk;
    unsigned long Time;
    unsigned long Next[8];
};

typedef const struct State SType;


SType FSM[11] = {
    {0x0C, PF1, 100, {goW, goW, waitW, waitW, waitW, waitW, waitW, waitW} },
    {0x14, PF1, 50,  {goW, goW, goS, goS, goWa, goWa, goWa, goWa} },
    {0x21, PF1, 100, {goS, waitS, goS, waitS, waitS, waitS, waitS, waitS} },
    {0x22, PF1, 50,  {goS, goW, goS, goW, goWa, goW, goWa, goW} },
    {0x24, PF3, 100, {goWa, off1, off1, off1, goWa, off1, off1, off1} },
    {0x24, OFF, 50,  {on1, on1, on1, on1, on1, on1, on1, on1} },
    {0x24, PF1, 50,  {off2, off2, off2, off2, off2, off2, off2, off2} },
    {0x24, OFF, 50,  {on2, on2, on2, on2, on2, on2, on2, on2} },
    {0x24, PF1, 50,  {off3, off3, off3, off3, off3, off3, off3, off3} },
    {0x24, OFF, 50,  {on3, on3, on3, on3, on3, on3, on3, on3} },
    {0x24, PF1, 50,  {goWa, goW, goS, goS, goWa, goW, goS, goS} },   
};

错误消息:

  

...

     

TableTrafficLight.c(155):错误:#67:预期为“}”

     

{0x0C,PF1,100,{goW,goW,waitW,waitW,waitW,waitW,waitW,waitW}},

     

...

编辑:结果我用分号结束了我的#define语句。哎呀。获得的经验教训。

1 个答案:

答案 0 :(得分:2)

在关闭大括号之前删除最后一个逗号(删除为突出显示):

SType FSM[11] = {
    {0x0C, PF1, 100, {goW, goW, waitW, waitW, waitW, waitW, waitW, waitW} },
    {0x14, PF1, 50,  {goW, goW, goS, goS, goWa, goWa, goWa, goWa} },
    {0x21, PF1, 100, {goS, waitS, goS, waitS, waitS, waitS, waitS, waitS} },
    {0x22, PF1, 50,  {goS, goW, goS, goW, goWa, goW, goWa, goW} },
    {0x24, PF3, 100, {goWa, off1, off1, off1, goWa, off1, off1, off1} },
    {0x24, OFF, 50,  {on1, on1, on1, on1, on1, on1, on1, on1} },
    {0x24, PF1, 50,  {off2, off2, off2, off2, off2, off2, off2, off2} },
    {0x24, OFF, 50,  {on2, on2, on2, on2, on2, on2, on2, on2} },
    {0x24, PF1, 50,  {off3, off3, off3, off3, off3, off3, off3, off3} },
    {0x24, OFF, 50,  {on3, on3, on3, on3, on3, on3, on3, on3} },
    {0x24, PF1, 50,  {goWa, goW, goS, goS, goWa, goW, goS, goS} }, //remove this comma  
};

变为:

SType FSM[11] = {
    {0x0C, PF1, 100, {goW, goW, waitW, waitW, waitW, waitW, waitW, waitW} },
    {0x14, PF1, 50,  {goW, goW, goS, goS, goWa, goWa, goWa, goWa} },
    {0x21, PF1, 100, {goS, waitS, goS, waitS, waitS, waitS, waitS, waitS} },
    {0x22, PF1, 50,  {goS, goW, goS, goW, goWa, goW, goWa, goW} },
    {0x24, PF3, 100, {goWa, off1, off1, off1, goWa, off1, off1, off1} },
    {0x24, OFF, 50,  {on1, on1, on1, on1, on1, on1, on1, on1} },
    {0x24, PF1, 50,  {off2, off2, off2, off2, off2, off2, off2, off2} },
    {0x24, OFF, 50,  {on2, on2, on2, on2, on2, on2, on2, on2} },
    {0x24, PF1, 50,  {off3, off3, off3, off3, off3, off3, off3, off3} },
    {0x24, OFF, 50,  {on3, on3, on3, on3, on3, on3, on3, on3} },
    {0x24, PF1, 50,  {goWa, goW, goS, goS, goWa, goW, goS, goS} } 
};