We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm trying to initialise a C-structure in like this:
struct S { uint8 a; uint8 b;}; struct S s1={.a=2};
Keil C51 v8.12 compiler does not like it:
error C141: syntax error near '.'
although my syntax is C99 conform. I couldn't find what the ANSI definition says about structure initialisation.
If I try
struct S {uint8 a; uint8 b;}; struct S s1={2};
it does not work either. This is also conform to C99.
Only
struct S {uint8 a;uint8 b;}; struct S s1={2,0};
is valid.
The problem is, that I don't want to explicitly initialise b, which in my real program has many bytes.
Of course, it is always possible to do
s1.a=2;
in the init part of the program. In my real program, the structure is to be stored in FLASH, therefore is no use to initialise like that.
Note that for simplicity, the reseved word "code" was not written.
Do you have an idea what is the source of the problem? Thanks you in advance for your ideas.
C51 is not C99 compliant.
Thanks Dan,
Still remains the question: is there a way to initialise only a part of the members of a structure (following ANSI standard)?
"... is there a way to initialise only a part of the members of a structure (following ANSI standard)?"
Pre-C99 allows partial structure initialization. You must initialize each member from the start of the structure up to where you want to stop. The remainder of the arithmetic typed members are initialized to zero and all pointer members are initialized to NULL.