On Sun, Jul 14, 2019 at 10:30:27AM +0200, Johannes Sixt wrote:

> Why would you re-order members? There's nothing wrong when a pointer is
> initialized by 0.

To appease tooling like "sparse" without having to remember to do
anything specific at the point-of-use sites. I'm open to the idea that
it is not worth appeasing sparse, but this may be a simple and practical
compromise.

> > But it does fall down
> > when the first element _has_ to be a struct (like, say, any user of our
> > hashmap.[ch] interface).
> 
> No, it does not. It is not necessary to spell out nested structs in the
> initializer.

Ah, that is news to me. I know that this compiles OK with "gcc -Wall",
but is it guaranteed by the standard?

-- >8 --
#include <stdio.h>

struct outer {
        struct {
                int x;
                char *y;
        } inner;
};

int main(void)
{
        struct outer foo = { 0 };
        printf("%d %p\n", foo.inner.x, foo.inner.y);
        return 0;
}
-- 8< --

If so, then I agree that "0" is a pretty good solution, tooling like
"sparse" aside. I do agree with your sentiment that "0" can be read as
"please zero-initialize this struct" and not really as an attempt to set
an initial member pointer to zero.

-Peff

Reply via email to