My question is how does the compiler here decide what type to use for that array of hex digits?
As with arrays and aggregate classes, the first initialiser initialises the first member; in this case i
. (Of course, unlike those things, it doesn't make sense to have more than one initialiser).
Why not just store it in the char[4] and skip the union? Is there some advantage of a union here that I don't see?
The purpose of this is to initialise the 4-byte integer, then use the char
array to examine the individual bytes to determine the memory order. If the most significant byte (0x01
) is stored in the first byte, then the system is "big-endian"; otherwise it's "little-endian" (or perhaps something stranger).