I have following code which I guess is assigning a value to constant struct
in header file:
struct madStruct {
uint8_t code;
uint8_t cluster;
};
typedef struct madStruct MadStruct;
and in C file
const MadStruct madStructConst = {
.code = 0x00,
.cluster = 0x01,
};
I would like to know what what does this code supposed to do?
This code does not compile in Visual Studio C++ 2010, how can I convert it so I can compile in both MingW and Visual Studio C++ 2010?
The syntax was introduced in C99 and allows the name of the members to be explicitly specified at initialisation (the
.codeand.clusterare known as designators). The initialisation assigns the value0x00to thecodemember and value0x01to theclustermember.VC only supports C89, so compiliation fails. As the
structonly has two members and both are being initialised you can replace the initialisation with:without the designators the members are initialised with the specified values in the order that the members are defined in the
struct. In this casecodeis assigned0x00andclusteris assigned0x01, the same as the initialisation with the designators.