1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| /*
Exemple de "tagged union"
*/
#include <stdio.h>
enum union_type
{
U_STRING,
U_NUMBER
};
/* La structure contient deux membres:
Une union et une valeur indiquant quel membre a été réglé. */
typedef struct tagged_union
{
enum union_type type;
union
{
char const * string;
int number;
} u;
} TAGGED_UNION;
void Afficher(TAGGED_UNION tu)
{
switch(tu.type)
{
case U_STRING: puts(tu.u.string); break;
case U_NUMBER: printf("%d\n", tu.u.number); break;
}
}
int main(void)
{
TAGGED_UNION tu;
tu.type = U_STRING;
tu.u.string = "Bonjour!";
Afficher(tu);
return 0;
} |