This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to initiase structures

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.

  • The problem is, that I don't want to explicitly initialise b

    if you don't want to explicitly initialize 'b', why not first read its value from flash, then store it in your structure?

  • Thanks for the answer Tamir.

    1- In my real program the structure is actually stored in the code section, not in xdata.

    2- Do you have an idea how to initialise only certain members of a structure without having to initialise all of them? In the C standard C99 this is described, but in Keil it does not seem to work (as you could see in the original message).

    Maybe you have an idea.

    Thanks.

  • So the problem is that an initializer for a member of the structure would be too long, so you would rather omit it?
    If that's the case, you could try some tricks with preprocessor macros. Namely, put together a macro that would expand into the big initializer.

  • 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.

  • So the problem is that an initializer for a member of the structure would be too long, so you would rather omit it?

    Exactly, Mike.

    Therefore something like (in C99 standard)

    struct S { uint8 a; uint8 b;};
    struct S s1={.a=2};

    would be perfect!

    Unluckily it seems that in the ANSI standard (used by C51) the initialiser list must contain all elements of your structure (array or union), without giving you the option to initialise selected items.

  • "it seems that in the ANSI standard (used by C51) the initialiser list must contain all elements of your structure (array or union), without giving you the option to initialise selected items."

    You don't have to initialise every element, but you can't have gaps in your initialiser list.

    So why not just put the element(s) that you don't want initialised at the end of the structure...?

  • Thanks, Andy.

    This is exactly the direction I was investigating and might be good enough.

    Actually I made a mistake in the original message

    If I try

    struct S {uint8 a; uint8 b;};
    struct S s1={2};

    it does not work either.

    Actually this works. It initialises 'a' to 2.

    So I will place all the members of the structure that must be initialised at the beginning.