Re: OT: Seed7 on OS X - WAS: Re: Seeking computer-programming job (Sunnyvale, CA)



[trimmed language-specific groups, who probably don't care about
my opinions on writing C; possibly comp.programming doesn't care
either, but I need to keep *some* group]

Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:

Please explain the potential for harm in:

struct tm foo = {0};

that causes this message:

foo.c:6: warning: missing initializer
foo.c:6: warning: (near initialization for `foo.tm_min')

and which vanishes when the code is changed to:

struct tm foo = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

struct tm is special, because it is defined by the standard C
library, not by the ordinary user of a C implementation, and
because the order and number of its members varies from one
implementation to another. The GNU C library implementation, for
example, adds two members that are not mentioned in the C
standard. That means that there is no way to specify a portable
initializer it without provoking a warning one way or another on
the GCC version above.

C and POSIX have a lot of structures in the same situation.

For structures that I define in my own code, I prefer to provide
a macro that specifies a full initializer, e.g.:

struct whoozit {
int quux;
const char *pow;
long int whazzle;
}
#define WHOOZIT_INITIALIZER {0, "yowza!", 1L}

That sidesteps the whole issue.
--
I love deadlines.
I love the whooshing noise they make as they go by.
--Douglas Adams
.



Relevant Pages

  • Re: #include __FILE__
    ... all but one C compiler that I use also allow the final ... >> define the size of a final field bringing the struct to ... Francois Grieu ...
    (comp.lang.c)
  • Re: Initializing compound type containing opaque type
    ... A workaround would be to rearrange the members of `struct foo' as ... which again provokes a missing initializer warning but is accepted. ...
    (comp.lang.c)
  • [PATCH V2] futex: add futex_q static initializer
    ... The futex_q struct has grown considerably over the last couple years. ... believe it now merits a static initializer to avoid uninitialized data ... With the key initializer built in, several of the FUTEX_KEY_INIT calls can ... return ret; ...
    (Linux-Kernel)
  • [PATCH V3] futex: add futex_q static initializer
    ... The futex_q struct has grown considerably over the last couple years. ... believe it now merits a static initializer to avoid uninitialized data ... With the key initializer built in, several of the FUTEX_KEY_INIT calls can ... return ret; ...
    (Linux-Kernel)
  • Re: incr MACRO
    ... elements will get an explicit initializer for the ... int, ... struct foo *tp; ... Assuming the first two members of the struct are named name and key, ...
    (comp.lang.c)