Initialize/reset struct to zero/null
The way to do such a thing when you have modern C (C99) is to use a compound literal.
a = (const struct x){ 0 };
This is somewhat similar to David's solution, only that you don't have to worry to declare an the empty structure or whether to declare it static
. If you use the const
as I did, the compiler is free to allocate the compound literal statically in read-only storage if appropriate.
Define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.
For example:
static const struct x EmptyStruct;
Here I am relying on static initialization to set my initial values, but you could use a struct initializer if you want different initial values.
Then, each time round the loop you can write:
myStructVariable = EmptyStruct;