|
Par
uknow
Mise à jour : 08/04/2011
582 visites depuis 7 jours,
classé 195/786
|
, et cette fois-ci c'est de fonctions qu'il s'agit.
, je vais vous faire un petit rappel sur les pointeurs :1 | int * pointeur; |
1 2 3 4 | int * ptrint; float * ptrfloat; char * ptrchar; double * ptrdouble; |
1 | void * pointeurGenerique; |
1 2 3 4 | void ma_fonction(void) { //instructions... } |
1 | pointeurSurFonction = &(ma_fonction); |
.1 | pointeurSurFonction = ma_fonction; |
1 2 3 4 5 6 | void ma_fonction (void) { printf("Hello world!\n"); } void * pointeurGenerique = ma_fonction ; |
, non ?1 | void ma_fonction(....); |
1 | void ma_fonction(void); |
1 | void (*pointeurSurFonction)(void); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | void ma_fonction(void); /*Prototype*/ void ma_fonction(void) /*La fonction*/ { printf("Hello world!\n"); } int main(void) { void (*pointeurSurFonction)(void); /*Déclaration du pointeur*/ pointeurSurFonction = ma_fonction; /*Sauvegarde de l'adresse de la fonction dans le pointeur adéquat*/ return 0; } |
1 | void ma_fonction(int argint); |
1 | void (*pointeurSurFonction)(int) |
1 | void ma_fonction(int argint,char * argchar,size_t leng); |
1 | void (*pointeurSurFonction)(int,char *,size_t); |
(ce sera toujours le cas d'ailleurs). A savoir :1 | pointeurSurFonction = ma_fonction; |
1 | int (*pointeurSurFonction)(void); |
1 | int * (*pointeurSurFonction)(double*,int); |
), l'appel aux fonctions est caractérisé par la présence de parenthèses '()'. Dans le cas général, on appellera une fonction à partir de son pointeur ainsi :1 2 3 4 | void afficherBonjour(void) { printf("Bonjour\n"); } |
1 2 3 4 5 6 7 8 9 | int main (void) { void (*pointeurSurFonction)(void); /*déclaration du pointeur*/ pointeurSurFonction = afficherBonjour; /*Initialisation*/ (*pointeurSurFonction)(); /*Appel de la fonction*/ return 0; } |
Bonjour |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void afficherBonjour(char * nom) { printf("Bonjour %s\n",nom); } int main (void) { void (*pointeurSurFonction)(char *); /*déclaration du pointeur*/ pointeurSurFonction = afficherBonjour; /*Initialisation*/ (*pointeurSurFonction)("zero"); /*Appel de la fonction*/ return 0; } |
Bonjour zero |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int saisirNombre(void) { int n; printf("Saisissez un nombre entier : "); scanf("%d",&n); return n; } int main (void) { int (*pointeurSurFonction)(void); /*déclaration du pointeur*/ int nombre; pointeurSurFonction = saisirNombre; /*Initialisation*/ nombre = (*pointeurSurFonction)(); /*Appel de la fonction*/ return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int saisirNombre(char * nom) { int n; printf("Bonjour %s saisie un nombre entier : ",nom); scanf("%d",&n); return n; } int main (void) { int (*pointeurSurFonction)(char *); /*déclaration du pointeur*/ int nombre; pointeurSurFonction = saisirNombre; /*Initialisation*/ nombre = (*pointeurSurFonction)("zero"); /*Appel de la fonction*/ return 0; } |
) cependant une telle pratique relève du non-portable. c'est pourquoi nous allons utiliser le pointeur type sur fonctions ainsi :1 2 3 4 5 6 7 8 9 | int fonction1(void) { return 1; } int (* fonction2(void))(void) { return fonction1; /*Ici le retour d'un pointeur sur fonction*/ } |
1 2 3 4 5 6 7 8 9 10 11 | int fonction1(double,double) { return 1; } int (* fonction2(char str[]))(double,double) { printf("%s",str); /*Affichage de la chaine passée en paramètre*/ return fonction1; /*Ici le retour d'un pointeur sur fonction*/ } |
, mais cette fois ci en mettant typedef à sa gauche, comme ceci :1 2 3 4 5 6 7 8 9 10 11 | typedef int (*ptrFonction)(void); int fonction1(void) { return 1; } ptrFonction fonction2(void) { return fonction1; /*Ici le retour d'un pointeur sur fonction*/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | typedef int (*ptrFonction)(double,double); int fonction1(double,double) { return 1; } ptrFonction fonction2(char str[]) { printf("%s",str); /*Affichage de la chaine passée en paramètre*/ return fonction1; /*Ici le retour d'un pointeur sur fonction*/ } |
).1 2 3 4 5 6 7 8 9 | void fonction1(int n) { printf("fonction 1 appel N° %d\n",n); } void fonction2(int n, void (*ptrfonction)(int)) { (*ptrfonction)(n); /*Appel de la fonction pointée par ptrfonction*/ } |
1 | fonction2(13,fonction1); |
fonction 1 appel N° 13 |
.1 | typedef void (*ptrfonction)(int); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void fonction1(int n) { printf("fonction 1 appel N° %d\n",n); } void fonction2(int n, ptrfonction ptr) { (*ptr)(n); /*Appel de la fonction pointée par ptrfonction*/ } int main(void) { fonction2(13,fonction1); } |
(vous vous en rappelez ? )
:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | double addition(double n1,double n2) { return n1 + n2; } double soustraction(double n1,double n2) { return n1 - n2; } double multiplication(double n1,double n2) { return n1 * n2; } double division(double n1,double n2) { return n1 / n2; } |
1 | double (*listeFonctions[4])(double,double) = {addition,soustraction,multiplication,division}; |
chouette non ?).1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | double (* affichMenu(void))(double,double) /*Le retour correspondant à un pointeur sur fonction*/ { int choix; /*La variable pour le choix*/ do{ printf("-------------------------------MENU--------------------------------------\n"); printf("Veuillez choisir une operation (en choisissant un nombre entre 1 et 4) :\n"); printf("1 pour addition\n"); printf("2 pour soustraction\n"); printf("3 pour multiplication\n"); printf("4 pour division\n"); printf("Votre choix : "); scanf("%d",&choix); }while(choix < 1 || choix > 4 ); /*En cas d'erreur de saisie*/ return listeFonctions[choix - 1]; /*On renvoi le pointeur sur la fonction de calcul choisie*/ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int main (void) { double (*fonctionDeCalcul)(double,double); /*déclaration du pointeur*/ double n1,n2; fonctionDeCalcul = affichMenu(); /*On charge la fonction de calcul choisie*/ printf("Saisissez les operandes : "); /*On lit les opérandes de notre calcul*/ scanf("%lf",&n1); scanf("%lf",&n2); printf("le resultat du calcul est : %f\n",(*fonctionDeCalcul)(n1,n2)); /*On appelle la fonction choisie et on affiche le résultat.*/ return 0; } |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <stdio.h> //---------Les prototypes----------- double addition(double n1,double n2); double soustraction(double n1,double n2); double multiplication(double n1,double n2); double division(double n1,double n2); double (* affichMenu(void))(double,double); //------Les fonctions de calcul------- double addition(double n1,double n2) { return n1 + n2; } double soustraction(double n1,double n2) { return n1 - n2; } double multiplication(double n1,double n2) { return n1 * n2; } double division(double n1,double n2) { return n1 / n2; } //------Le tableau de pointeur sur fonctions (global)------ double (*listeFonctions[4])(double,double) = {addition,soustraction,multiplication,division}; //--------Fonction du menu-------- double (* affichMenu(void))(double,double) /*Le retour correspondant à un pointeur sur fonction*/ { int choix; /*La variable pour le choix*/ do{ printf("-------------------------------MENU--------------------------------------\n"); printf("Veuillez choisir une operation (en choisissant un nombre entre 1 et 4) :\n"); printf("1 pour addition\n"); printf("2 pour soustraction\n"); printf("3 pour multiplication\n"); printf("4 pour division\n"); printf("Votre choix : "); scanf("%d",&choix); }while(choix < 1 || choix > 4 ); /*En cas d'erreur de saisie*/ return listeFonctions[choix - 1]; /*On renvoi le pointeur sur la fonction de calcul choisie*/ } //--------Le main---------- int main(void) { double (*fonctionDeCalcul)(double,double); /*déclaration du pointeur*/ double n1,n2; /*Les opérandes*/ fonctionDeCalcul = affichMenu(); /*On lit la fonction choisie*/ printf("Saisissez les operandes : "); /*On demande la saisie des opérandes*/ scanf("%lf",&n1); scanf("%lf",&n2); printf("le resultat du calcul est : %f\n",(*fonctionDeCalcul)(n1,n2)); /*On affiche le résultat*/ return 0; } |
.