1

I am trying to work out what is happening in a program that has no documentation whatsoever. Here's part of the code:

typedef struct
{
    UInt8 Access;
    UInt8 PDOMapping;
    UInt8 Size;
    void* Data;
    CheckValue Check;
    AccessEvent ReadEvent;
    AccessEvent WriteEvent;
}ObjectValue;

typedef struct
{
    UInt16          Index;
    UInt8           Code;
    ObjectValue*    Value;
}Object;

const UInt8 MaxSubIndex6400 = 1;    
ObjectValue Object6400Value[] = 
{
    {ACCESS_RO, MAP_NONE, 1, (void*)&MaxSubIndex6400, null, null},
    {ACCESS_RO, MAP_NONE, sizeof(Index6400Buffer), (void*)&Index6400Buffer, null, null, null}, 
};

The array of Object6400Value contains two items, but they have a different number of entries. Is this legal?

unwind
  • 391,730
  • 64
  • 469
  • 606
Dirk Bruere
  • 237
  • 2
  • 15
  • 1
    What is `null`? – unwind Apr 10 '17 at 09:38
  • 1
    Ah; I see that the 1st entry has no initializer for `WriteEvent`. If I remember well, then any initializer not provided will result in initializing to zero. – Paul Ogilvie Apr 10 '17 at 09:40
  • 1
    Yes, due to sorrounding `{}` that isolate a single `ObjectValue` item. Non initialized members are seto to `0`. – LPs Apr 10 '17 at 09:41
  • @unwind #define null (void*)0 – Dirk Bruere Apr 10 '17 at 09:50
  • 1
    @DirkBruere That's seems ripe for a search-and replace to `NULL`, and add the proper `#include` (I like ``, but it's defined by several, see [this answer for the list](http://stackoverflow.com/a/12024299/28169)) to all relevant sources. No point in dragging around something like that. – unwind Apr 10 '17 at 10:15

2 Answers2

4

Yes, this is legal.

The non-mentioned members will simply be default-initialized to 0.

The C11 draft standard says (in §6.7.9.21, page 141):

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Yes, except this was written (apparently) in 2010 and earlier. Multiple nested structures, function pointers, variable names - none of it documented. Nightmare – Dirk Bruere Apr 10 '17 at 09:47
2

If there are too few initialisers for an aggregate, the remaining ones are initialised with zero.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82