Question Details

No question body available.

Tags

c struct macros

Answers (1)

Accepted Answer Available
Accepted Answer
March 12, 2026 Score: 8 Rep: 1 Quality: High Completeness: 50%
#include 

struct s1 { int x; }; struct s2 { int y; };

#define MYMACRO(val) Generic((val), \ struct s1: handles1, \ struct s2: handles2 \ )(&(val))

void handles1(struct s1 *s) { s->x = 10; } void handles2(struct s2 *s) { s->y = 20; }

int main() { struct s1 a; struct s2 b; MYMACRO(a); MYMACRO(b); printf("a.x = %d, b.y = %d\n", a.x, b.y); }

_Generic does compile-time type dispatch — each branch is only compiled for the matching type, so no "missing member" errors.