Corey Starbird Corey Starbird - 2 months ago 16
C Question

Error creating Linked List with array inside list definition

The following code gives me an error where

Stype FSM[7]
is defined. In the
{0x30, 700...
line the compiler says
error: expected '}'
and
error: expected ';' at end of declaration


According to our teacher's sample code, this should work. However I'm seeing in other places that we should define the arrays of "next states" outside of the struct. Is this the case?

const struct State {
unsigned int out;
unsigned int time; //10ms units
const struct State *next[16];
};
typedef const struct State SType;

//define your states here e.g. #define stateName 0, etc.
SType FSM[7];

#define goW &FSM[0];
#define go1 &FSM[1];
#define slowW &FSM[2];
#define slow1 &FSM[3];
#define crossW &FSM[4];
#define cross1 &FSM[5];
#define crossB &FSM[6];

//Declare your states here

SType FSM[7] = {
{0x30, 700, {goW, goW, slowW, slowW, slowW, slowW, slowW, slowW, cross1, cross1, slowW, slowW, slowW, slowW, slowW, slowW}},
{0x84, 700, {slow1, slow1, go1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1, slow1}},
{0x50, 200, {go1, go1, go1, go1, crossW, crossW, crossW, crossW, cross1, cross1, cross1, go1, crossB, crossB, crossB, crossB}},
{0x88, 200, {goW, goW, goW, goW, crossW, crossW, crossW, goW, cross1, cross1, cross1, cross1, crossB, crossB, crossB, goW}},
{0x92, 400, {goW, goW, go1, goW, crossW, goW, go1, go1, cross1, cross1, cross1, cross1, crossB, crossB, crossB, crossB}},
{0x31, 400, {goW, goW, go1, goW, crossW, crossW, crossW, crossW, cross1, cross1, go1, goW, crossB, crossB, crossB, crossB}},
{0x93, 400, {goW, goW, go1, goW, crossW, goW, go1, goW, cross1, cross1, go1, goW, crossB, goW, go1, go1}}
};

Answer

Get rid of ; after each #define. These are not C statements and don't need semicolons.