0

I have a struct defined as:

 #pragma pack(push,1)
 typedef struct {
     uint8_t source_ID;
     uint8_t dest_ID;
     union {
         uint8_t node_ID;

         struct {
             unsigned int dest   : 4; 
             unsigned int source : 4;
         };
     };
 } IDs_t; 
 #pragma pack(pop)

When I declare one of these structs I do the following:

IDs_t IDs = {         
    .source_ID = TWI,
    .dest_ID = APP,
    .source = INTERNAL,
    .dest = LISTENER,
};

The values I'm setting them equal to are #define values.

When I compile the code it works, no errors.

If I declare one of the structs like this:

IDs_t IDs = {         
    .source_ID = TWI,
    .dest_ID = APP,
    .dest = LISTENER,
    .source = INTERNAL,
};

Then I get the following errors:

501:9: error: missing initializer [-Werror=missing-field-initializers] 501:9: error: (near initialization for 'IDs...source') [-Werror=missing-field-initializers]

When all I did was reorder the element declarations. Is this not allowed? I can't seem to find the answer, but I have a suspicion. My suspicion is that since I am using an ARM core from ST, the memory is in little endian format. Which means that the source element is the LSB of the node_ID element. Therefore I must declare the LSB first and then the MSB, which would be the dest element.

Is my suspicion correct? Or is there another reason for these errors?

cDreamer
  • 385
  • 5
  • 18
  • 1
    You are using compiler flags that generate errors for valid code. By default this message is a warning, meant to alert you if you initialized some but not all of the members of an aggregate – M.M Jun 26 '18 at 21:45
  • It would improve the question to post a [MCVE](http://stackoverflow.com/help/mcve), and select a language tag (either C or C++ according to which sort of compiler you are using) – M.M Jun 26 '18 at 21:46
  • @M.M I have updated my question to reflect that I am using C language. – cDreamer Jun 27 '18 at 01:34
  • @M.M Why are the errors even being generated, or why would there be a warning in the first place? – cDreamer Jun 27 '18 at 01:35
  • The missing-initializer warning doesn't work very well when used with designated initializers – M.M Jun 27 '18 at 01:36
  • You need to pay close attention to [C11 Standard - 6.7.9 Initialization](http://port70.net/~nsz/c/c11/n1570.html#6.7.9) and particularly [C11 Standard - 6.7.9 Initialization (p20)](http://port70.net/~nsz/c/c11/n1570.html#6.7.9p20). – David C. Rankin Jun 27 '18 at 01:49
  • @DavidC.Rankin I don't see how that comment is helpful, perhaps you could elaborate – M.M Jun 27 '18 at 01:58
  • 1
    p20 Loosely discusses how the compiler must address subaggregate unions, specifically *"only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union"*, while there is nothing wrong with the initialization, depending on how the compiler interprets the un-brace separate subaggregate initialization the compiler may not discriminate between the named bitfield initializers and the first member of the contained union. Which supports your point of the compiler warning may not work very well in this case. – David C. Rankin Jun 27 '18 at 02:09
  • Yeah, the trouble is that often it is intentional to not initialize some members (especially when using designated initializers), and the compiler doesn't really have any reliable way of knowing if you intentionally omitted a member or not. In this exact case it seems bogus that changing the order of initializers changes the presence of the warning despite the fact that that doesn't change the program semantics; it's probably a symptom of some ad hoc logic for guessing intention. If enabling this warning then, in my experience, you do end up mangling your code to silence warnings. – M.M Jun 27 '18 at 04:46

0 Answers0