Topic : const struct initialization with names

Forum : ST7/STM8

Original Post
Post Information Post
March 21, 2011 - 10:17pm
Guest

In my code I want to store default values in a const struct, but I want to use the member names during initialization to prevent future bugs in the case of a typo. ("designated initializer list" is what that's called, right?)

struct
{
   u32 serial_number;
   u8 model;
   u8 mfg_year;
   u8 mfg_month;
   u8 mfg_day;
} info;

const struct info x
{
   .serial_number = 0,
   .model = 0,
   .mfg_year = 11,
   .mfg_month = 3,
   .mfg_day = 1
};

But I get

Quote:
*** ERROR C000 IN LINE xxxx OF yyyy: Character '}' missing
*** ERROR C131 IN LINE zzzz OF yyyy: unbalanced #if-endif controls

The following seems to work though:
const struct info x
{
   0,
   0,
   11,
   3,
   1
};

What are my options?

Replies
Post Information Post
+1
0
-1
March 22, 2011 - 10:13am
Raisonance Support Team

Hi,

This is NOT valid C code. Your best option is to stick to the standard (ISO C90), and use structure fields as ordered. C does not offer structure fields access by their names.

Best regards,