union.c
00001 #include <stdio.h>
00002
00003 struct jour {
00004 enum {CHAR_STATE, INT_STATE} indicateur;
00005 union {
00006 char lettre;
00007 int numero;
00008 } day;
00009 };
00010
00011
00012 void affiche_jour(struct jour d, char * name) {
00013 switch (d.indicateur) {
00014 case CHAR_STATE: printf("%s = %c\n",name,d.day.lettre); break;
00015 case INT_STATE : printf("%s = %i\n",name,d.day.numero); break;
00016 default: printf("erreur, union en état inconnu!\n");
00017 }
00018 }
00019
00020 int main()
00021 {
00022 struct jour ex1, ex2;
00023
00024 ex1.indicateur = CHAR_STATE;
00025 ex1.day.lettre = 'j';
00026 affiche_jour(ex1, "ex1");
00027 ex2.indicateur = INT_STATE;
00028 ex2.day.numero = 4;
00029 affiche_jour(ex2, "ex2");
00030 return 0;
00031 }
00032
00033