A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
C99 designated initializers let you specify which member or index you are initializing by name. For structs: {.x = 3.0, .y = 4.0}. For arrays: {[0] = 10, [4] = 50}. Unnamed members are zero-initialized. The order does not matter. Adding a new member to the struct does not break existing initializers. This is a readability and maintainability improvement over positional initialization, and it is the style used in the Linux kernel and most modern C codebases.
#include <stdio.h>typedef struct {int x;int y;int z;int w;} Vec4;int main(void) {Vec4 positional = {1, 2, 3, 4};Vec4 designated = {.x = 10, .z = 30};Vec4 reordered = {.w = 400, .x = 100, .y = 200, .z = 300};printf("positional: %d %d %d %d\n",positional.x, positional.y, positional.z, positional.w);printf("designated: %d %d %d %d\n",designated.x, designated.y, designated.z, designated.w);printf("reordered: %d %d %d %d\n",reordered.x, reordered.y, reordered.z, reordered.w);int sparse[10] = {[0] = 1, [5] = 50, [9] = 90};printf("\nsparse array: ");for (int i = 0; i < 10; i++) {printf("%d ", sparse[i]);}printf("\n");return 0;}
The designated initializer for Vec4 set x and z. y and w were automatically zeroed. The reordered initializer specified members in a different order than the struct declaration. The compiler placed them correctly. The sparse array initialized indices 0, 5, and 9. The rest are zero. No loop needed. No memset. One declaration.
Suppose you add a new member to Vec4 between y and z. Every positional initializer in the codebase now assigns values to the wrong members. The compiler does not warn you because the types still match. With designated initializers, the existing code is unaffected. The new member gets zero. The named members go to the right place. This is a real maintenance advantage in large codebases.
Designated initializers make struct initialization self-documenting and resilient to member reordering. Use them by default in C99 and later code. The positional form is only necessary for C89 compatibility. Next: flexible array members, the C99 feature that lets a struct's last member have a runtime-determined size.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.