Reinitialize timeval struct

BSchlinker picture BSchlinker · Jun 24, 2011 · Viewed 15.9k times · Source

How can I reinitialize a timeval struct from time.h?

I recognize that I can reset both of the members of the struct to zero, but is there some other method I am overlooking?

Answer

R.. GitHub STOP HELPING ICE picture R.. GitHub STOP HELPING ICE · Jun 24, 2011

The completely correct and portable (albeit C99) way to zero-initialize arbitrary (possibly aggregate) types:

myTime = (struct timeval){0};

This works even for structures that contain pointers and floating point members, even if the implementation does not use all-zero-bits as the representations for null pointers and floating point zeros.