Renvoyer le pointeur aux erreurs de fonction

On m’a demandé de créer un code qui réorganiserait 3 entiers entrés en ordre croissant / décroissant en utilisant des pointeurs.

Je dois utiliser la fonction order() pour renvoyer un pointeur vers la fonction ascending3() ou descending() , en fonction de la valeur de ‘e’ entrée.

Je continue à recevoir une erreur sur la ligne spécifiée dans la fonction readInts () et je ne suis pas sûr de savoir comment le réparer.

LES ERREURS

 lvalue required as unary '&' operand" -- The error for `ptr = &(order(e))` warning: assignment makes pointer from integer without a cast -- The error for `ptr=order(e)` 

Code du pointeur

 void readInts(){ int *a,*b,*c; char e; int (*ptr1)(int*,int*,int*); int result; printf("Enter Integer 1:"); scanf("%d", a); printf("Enter Integer 2:"); scanf("%d", b); printf("Enter Integer 3:"); scanf("%d", c); printf("Enter either A or D:"); scanf(" %c", &e); ptr1 = &(order(e)); /*ERROR HERE*/ result = (*ptr1)(a,b,c); printf("%d %d %d", a, b, c); } 

Les fonctions

 int ascending3(int* x, int* y, int* z) { /*removed sorting code for the sake of shortening the question*/ *x=smallest; *y=middle; *z=largest; } int descending(int* x, int* y, int* z) { int swap; ascending3(x,y,z); swap=*x; *x=*z; *z=swap; } int (*order(char e))(int*x ,int*y,int*z) { if(e=='a') { return ascending3; } else if(e =='d') { return descending; } return; } 

Une fonction ne peut pas retourner une fonction. Pour cette raison, vous ne pouvez pas appliquer l’ address of opérateur ( & ) au résultat de votre fonction (pour récupérer une adresse). Mais une fonction peut renvoyer un pointeur sur une fonction.

Le nom d’une fonction dans C (préfixé ou non par l’opérateur & ) est toujours défini comme adresse de la fonction, qui est un pointeur sur cette fonction.

Le code correct est:

 int ascending3(int *x, int *y, int *z); int descending(int *x, int *y, int *z); typedef int (*fn)(int *x, int *y, int *z); fn order(char e) { if (e == 'a') { return ascending3; } else if (e == 'd') { return descending; } return NULL; } void readInts(void) { int *a, *b, *c; char e; fn ptr1; int result; printf("Enter Integer 1:"); scanf("%d", a); printf("Enter Integer 2:"); scanf("%d", b); printf("Enter Integer 3:"); scanf("%d", c); printf("Enter either A or D:"); scanf(" %c", &e); ptr1 = order(e); result = (*ptr1) (a, b, c); printf("%p %p %p", a, b, c); } 

Où j’ai utilisé un typedef pour déclarer le type de notre pointeur de fonction (et aussi 2 prototypes pour les fonctions de classement).

Si vous aimez avoir l’astérisque pour mieux montrer la nature du pointeur de notre type, vous pouvez définir fn comme fonction (pas un pointeur sur la fonction):

 typedef int (fn)(int *x, int *y, int *z); 

Vous pouvez donc utiliser la notation astérisque:

 typedef int fn(int *x, int *y, int *z); fn *order(char e) { if (e == 'a') { return ascending3; } else if (e == 'd') { return descending; } return NULL; } void readInts(void) { int *a, *b, *c; char e; fn *ptr1; int result; printf("Enter Integer 1:"); scanf("%d", a); printf("Enter Integer 2:"); scanf("%d", b); printf("Enter Integer 3:"); scanf("%d", c); printf("Enter either A or D:"); scanf(" %c", &e); ptr1 = order(e); result = (*ptr1) (a, b, c); printf("%p %p %p", a, b, c); }