I have typdef'd a struct, and immediately below this I've simultaneously declared and initialised the variables I want using the typedef'd struct.
When I try to compile the code, there are no error messages relating to 'hi_aud', but the rest of the structs bring up the error 'error: ";" expected'. The array also brings up this warning, plus 'error: "}" expected'.
I'm using Hi-Tech C compiler v., which uses the C90 ANSI C standard.
/* Due to C90, do not change order of vars in struct or else code will explode
*/
typedef struct alarm {
const uint_fast32_t pattern[];
const uint_fast8_t size;
void (*toggle)(void);
void (*off)(void);
bool on;
bool init;
uint_fast8_t pos;
uint_fast16_t start;
uint_fast8_t overflows;
};
static struct alarm hi_aud {
[108, 27, 108, 20, 108, 12, 108, 5],
sizeof(hi_aud.pattern) / sizeof(*hi_aud.pattern),
&sounder_toggle,
&sounder_off,
};
static struct alarm med_aud {
[255, 50, 50, 255],
sizeof(med_aud.pattern) / sizeof(*med_aud.pattern),
&sounder_toggle,
&sounder_off,
};
static struct alarm lo_aud {
[255],
sizeof(lo_aud.pattern) / sizeof(*lo_aud.pattern),
&sounder_toggle,
&sounder_off,
};
static struct alarm hi_vis {
[255],
sizeof(hi_vis.pattern) / sizeof(*hi_vis.pattern),
&hi_vis_toggle,
&hi_vis_off;
};
static struct alarm med_vis {
[255],
sizeof(med_vis.pattern) / sizeof(*med_vis.pattern),
&med_vis_toggle,
&med_vis_off,
};
static struct *alarms = {&hi_aud, &med_aud, &lo_aud, &hi_vis, &lo_vis};
static uint_fast8_t alarms_siz = sizeof(alarms) / sizeof(*alarms);
edit When I use the '{}' brackets to initialise the array, another error "error: no identifier in declaration" comes up. This does not happen when I use the '[]' brackets.
Inside a strcut's defintion the elements' defintions are separated by not the issue :};
not be ,
.
Prior to C99 the initialiser list may not end by confused by C99 change for enums. Ending it with a ,
.;
is always wrong. And there needs to be a =
between the variable and its initialiser.
To initialise a struct's array member use curly braces ({}
), not brackets ([]
).
const uint_fast32_t pattern[];
is not a complete array definition.
Use const uint_fast32_t pattern[MAX_SOMETHING];
instead.