prev up next   top/contents search

comp.lang.c FAQ list · Question 2.19

Q: What's the difference between a structure and a union, anyway?

A: A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. (You can also cheat by writing to one field and reading from another, to inspect a type's bit patterns or interpret them differently, but that's obviously pretty machine-dependent.) The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members. (In both cases, the size may be increased by padding; see questions 2.12 and 2.13.)

References: ISO Sec.
H&S Sec. 5.7 pp. 140-145 esp. Sec. 5.7.4

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North