Designated Initializers (C99) in C
Designated initializers name fields at initialization: struct Point p = {.x = 1, .y = 2}; and int a[5] = {[2] = 7}; — self-documenting, order-independent, and robust against struct layout changes. Unmentioned members zero-initialize automatically, so partial initialization is safe by construction.
This one feature kills whole bug classes: swapped positional arguments, forgotten fields after struct growth, and sparse array tables. Any initializer that isn't trivially positional should designate.
Name struct fields
.x = 1, .y = 2 designates each member — order-free and immune to later field reordering.
Index array slots
[2] = 7 initializes just slot 2 — sparse tables and register maps without counting commas.
Rest zeroes itself
Unmentioned members and slots initialize to zero — partial designators are complete and safe.
Range designators (GNU)
[0 ... 9] = -1 fills runs in GNU C — handy, but non-standard, so guard with portability needs.
#include <stdio.h> typedef struct { int x; int y;} Point; int main(void) { Point p = {.y = 9, .x = 1}; int a[5] = {[4] = 7}; printf("%d,%d %d\n", p.x, p.y, a[4]); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.