S
Shao Miller
In comparing
union MyUnion ux = {"C Language"};
to separate declaration and initialization of ux, I see
efficiency as of far less importance than correctness, clarity,
and brevity, on which counts the combined statement excels.
Agreed.
Of course the combined statement *is* far more efficient, probably
saving over 10ns of runtime per program execution relative to
initialization by strcpy(), and probably over 50ns relative to
strncpy(), and saving easily a handful of bytes in program size.
I'm not sure that I understand why, for the case of 'strncpy'. Is it
because of the overhead of calling a function?
6.7.8p19 includes "... all subobjects that are not initialized
explicitly shall be initialized implicitly the same as objects that have
static storage duration."
Does that mean that the remaining elements of the 'ux' array are set to
null character values, just the same as 'strncpy' does?
I've seen a Microsoft compiler complain about:
void func(void) {
char foo[100] = {0};
return;
}
if the module providing 'memset' was not linked-with. It seems to me
that for this implementation, initializing with a string literal might
possibly employ 'strncpy' for the same reason as it employs 'memset' for
the initialization shown just above.