struct initialization, pointers, doubles



Hello everyone,

Are the following snippets well-defined

unsigned u;
memset(&u, 0, sizeof u); /* now u == 0 */

and

int i;
memset(&i, 0, sizeof i); /* now i == 0 */

Is the answer the same in C90 and C99?

AFAIU, on some (many) platforms, the representation of NULL
and 0.0 is all-bits 0, but there is no such guarantee.

Consider

struct foo { int i; void *p; double d; };

struct foo bar = { 0 };

On a platform where NULL and 0.0 are all-bits 0, the compiler
is free to change the statement to

memset(&bar, 0, sizeof bar);

But if that were not the case, the compiler would have to
output the machine-code equivalent of

bar.i = 0;
bar.p = NULL;
bar.d = 0.0;

(which might be much slower if the struct holds e.g. arrays of
pointers and doubles).

Is my understanding correct?

Regards.
.