CKTIS / C
records and aggregates — concept C23
`struct` groups named members, `union` shares storage among members, and arrays hold one element type; designated initializers reduce order dependence, but padding and layout must not be assumed without an ABI.
Version: C23
Syntax
records and aggregatesstruct groups named members, union shares storage among members, and arrays hold one element type; designated initializers reduce order dependence, but padding and layout must not be assumed without an ABI.
Use designated initializers such as struct User u = {.id = 7, .name = "Ada"}; so code depends less on member order. For a public type, document ownership of pointer members; copying the whole structure does not duplicate pointed-to data.
A flexible array member must be last and needs extra allocation with overflow checks. A union stores one member representation at a time, padding may contain unspecified bytes, and the address of the first structure member has special guarantees that do not extend to every member.
A tagged union pairs a union with a separate enum tag; every variant write must update the tag and every read must first select the matching member, because reading another representation can be non-portable or trap. A self-referential structure may contain a pointer to its own still-incomplete type inside its declaration, but not a direct instance of itself. Lists and trees need explicit node ownership, cleanup after partial allocation, and a policy for cycles.
Examples
#include <stdio.h>
struct User { int id; const char *name; };
int main(void) { struct User user = {.id = 7, .name = "Ada"}; printf("%d %s\n", user.id, user.name); return 0; }
Result
7 Ada
`struct` groups named members, `union` shares storage among members, and arrays hold one element type; designated initializers reduce order dependence, but padding and layout must not be assumed without an ABI.