Ben Bacarisse
2/10/2011 12:36:00 PM
Voyageur Galactique <voyageur.galactique@gmail.com> writes:
> Suppose I have a struct T .
>
> struct T* t = malloc(sizeof(struct T));
>
> I want to initialize t to zero.
The page that someone else has pointed you to explains that "zero" is,
sadly, slightly ambiguous in this context.
> I did it in the following way :
>
> *t = (struct T) {0};
>
> I would like to know if it is safe ?
Yes, that is absolutely safe. It will work even when a null pointer (or
a floating point zero) is not all bits zero.
> From my experience, it works well with gcc, but is it guaranteed to
> work on other compiler ?
The trouble is that it's C99 -- the newest C standard -- and not all
compilers implement C99.
--
Ben.