1. 8

  2. 2

    another interesting subject is zero initializing without memset():

    struct foo x = {};


    x = (struct foo){};


    x = (struct foo){ .a = 23, };

    though it may omit padding bytes

    1. 10

      Note that empty initializer lists (struct foo x = {}) are not valid in ISO C. The portable equivalent is struct foo x = {0}, which initializes the first field to 0, and all unspecified fields are initialized to 0. This works even for non-aggregate types, for example int x = {0}.

      1. 1

        didn’t know this works for int, too, wow!

        what about this case though:

        struct foo { struct { int x; int y; } sub; int z; };

        struct foo f = {0}; f = (struct foo){0};

        Since assigning 0 to an aggregate type (f.sub = 0) does not make sense, so far I thought that i would spare compiler errors by just omitting that zero. colleagues did complain, but the point that {0} seems to work no matter the nature of the type was never made. Does x = (struct x){0}; always work for all thinkable aggregate and nested aggregate types??