Download Le langage C
Transcript
Listing 15.9 : Utilisation d’un pointeur vers une fonction pour choisir entre plusieurs
fonctions à appeler
1: /* Utilisation d’un pointeur pour appeler différentes fonctions. */
2: #include <stdio.h>
3: #include <stdlib.h>
4:
5: /* prototypes des fonctions. */
6: void func1(int x);
7: void one(void);
8: void two(void);
9: void other(void);
10:
11: int main()
12: { int a;
13:
14: for (;;)
15: { puts("\nTapez un entier compris entre 1 et 10, 0 pour quitter");
16: scanf("%d", &a);
17: if (a == 0) break;
18: func1(a);
19: }
20: exit(EXIT_SUCCESS);
21: }
22: void func1(int x)
23: { void (*ptr)(void);
24:
25: if (x == 1) ptr = one;
26: else if (x == 2) ptr = two;
27: else ptr = other;
28:
29: ptr();
30: }
31:
32: void one(void)
33: { puts("Vous avez tapé 1");
34: }
35:
36: void two(void)
37: { puts("Vous avez tapé 2");
38: }
39:
40: void other(void)
41: { puts("Vous n’avez tapé ni 1 ni 2");
42: }
Tapez un entier compris entre 1 et 10, 0 pour quitter
1
Vous avez tapé 1
Tapez un entier compris entre 1 et 10, 0 pour quitter
2
Related documents
1 - rue des sabots
LATEX... pour le prof de maths !
Prolog III - Colmerauer, Alain
47 F2 22US 04 - Support On Line
English Only - Graphic Communications
TH-1130M manual_Eng.cdr
47 F2 21US REV04 - Support On Line
[U4.82.03] Opérateur CALC_G
ATTELLE DE GENOU - Dossier technique -
[U4.36.03] Opérateur CALC_INTE_SPEC
Samsung SP-H03 Manuel de l'utilisateur
RECEPTEUR TNT A DOUBLE TUNER