c++structtimeval

Reinitialize timeval struct


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?


Solution

  • 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.