Learn C
#include "structstypes/designated_init.h"

Designated Initializers (C99) in C

#concept

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.

#how it works
1

Name struct fields

.x = 1, .y = 2 designates each member — order-free and immune to later field reordering.

2

Index array slots

[2] = 7 initializes just slot 2 — sparse tables and register maps without counting commas.

3

Rest zeroes itself

Unmentioned members and slots initialize to zero — partial designators are complete and safe.

4

Range designators (GNU)

[0 ... 9] = -1 fills runs in GNU C — handy, but non-standard, so guard with portability needs.

#example
#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;
}
Output
1,9 7
1Designators fill y then x regardless of order — the struct holds 1, 9 correctly.
2[4] = 7 sets only the last slot; slots 0–3 zero themselves.
31,9 7 prints — named initialization surviving reorder and sparsity.
#use cases
Struct initialization that survives field additions and reorderings
Sparse lookup tables initialized by meaningful index
Config structs with mostly-default fields set selectively
Replacing positional initializers everywhere order isn't obvious

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.