A struct groups named fields; a typedef creates a convenient type alias. A struct is copied by value, including any pointer fields, but copying a pointer does not copy its target.
#include <stdio.h>
typedef struct { int pages; int copies; } PrintJob;
int main(void) {
PrintJob a = {.pages = 2, .copies = 3};
PrintJob b = a;
b.pages = 7;
printf("%d %d\n", a.pages, b.pages);
return 0;
}
Output: 2 7. For a pointer PrintJob *p, p->pages means (*p).pages. The compiler may insert padding between fields or at the end to satisfy alignment. For illustration only, a char followed by a four-byte aligned int could have three padding bytes:
[ char ][ padding padding padding ][ int int int int ]
Actual sizes, alignments and padding vary. sizeof includes padding. Reordering fields can reduce padding on a particular implementation but changes layout and is not a portable wire format.
An enum names integer constants: enum Kind { NUMBER, TEXT = 4, VECTOR }; gives 0, 4, 5. An enum does not enforce membership at runtime. A switch selects a matching case; break prevents fallthrough, and default handles other values. An enum’s compatible integer type and size are implementation-dependent.
A union overlays member storage. A tag stored separately in a struct tells code which member it should interpret. Its size is at least large enough for each member and can include padding. Reading a different member reinterprets representation in C and can be nonportable or yield an invalid representation; never use an arbitrary reinterpreted pointer. A struct of bytes is not automatically a portable network packet: padding, alignment and byte order matter. In a hypothetical four-octet integer 0x12345678, big endian stores 12 34 56 78 in increasing addresses; little endian stores 78 56 34 12. Encode protocol fields explicitly.