IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

pointeurs sur fonctions


Sujet :

C

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    75
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 75
    Points : 43
    Points
    43
    Par défaut pointeurs sur fonctions
    Bonjour,

    Je debute en langage C,mais je me penche sur une fonction avancee
    les pointeurs sur fonctions.
    Je dois afficher le contenu d' une variable variadique en fonction de son type char long int char *
    pour le moment je ne me retrouve pas dans le code que j ' ai construit
    merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
     
    void affint(p_list,int)
    {
      int i;
      i = va_arg(p_list, int);
      printf("%d\n", i);
    }
    void affilong(p_list,long)
    {
      long l;
      l = va_arg(p_list, long);
      printf("%ld\n", l);
    }
    void affidouble(p_list,double)
    {
      double f;
      f = va_arg(p_list, double);
      printf("%f\n", f);
    }
    void affichar(char *)
    {
      char *s;
      s = va_arg(p_list, char *);
      printf("%s\n", s);
    }
     
    int lecture(char *format, ...){
      void (*point1)(int);
       void (*point2)(long);
        void (*point3)(double);
         void (*point4)(char *);
       va_list p_list;
       point1=&affint;
       point2=&affilong;
       point3=&affidouble;
       point4=&affichar;
       va_start(p_list,format);
       (*point1)();    
       (*point2)();
       (*point3)();
       (*point4)();
       return (0);
     }
     
    int main(){
      lecture("restore",23, 678);
      return EXIT_SUCCESS; /*return 0*/;
    }

  2. #2
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Je ne vois pas vraiment l'intérêt de ce que tu essaies de réaliser ici. En particulier, je ne vois pas la valeur ajoutée de passer par les pointeurs de fonction. Toutefois, ceci fonctionne:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
     
    void affint(int i)
    {
        printf("int: %d\n", i);
    }
    void affilong(long l)
    {
        printf("long: %ld\n", l);
    }
    void affidouble(double f)
    {
        printf("double: %f\n", f);
    }
    void affichar(const char *s)
    {
        printf("STRING: %s\n", s);
    }
     
    int lecture(char *format, ...)
    {
        void (*point1)(int);
        void (*point2)(long);
        void (*point3)(double);
        void (*point4)(const char *);
     
        va_list p_list;
     
        point1 = &affint;
        point2 = &affilong;
        point3 = &affidouble;
        point4 = &affichar;
     
        va_start(p_list, format);
        point1(va_arg(p_list, int));
        point2(va_arg(p_list, long));
        point3(va_arg(p_list, double));
        point4(va_arg(p_list, const char *));
     
        va_end(p_list);
     
        return 0;
    }
     
    int main(void)
    {
        lecture("NOT USED FOR THE MOMENT", 100, 200, 3.1415, "DVP.com");
        return EXIT_SUCCESS;
    }
    Avec mes meilleures salutations

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  3. #3
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Ou alors, si tu veux déléguer aux fonctions le soin d'appeler va_arg(), on a:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
     
    void affint(va_list *p)
    {
        int i = va_arg(*p, int);
        printf("int: %d\n", i);
    }
    void affilong(va_list *p)
    {
        long l = va_arg(*p, long);
        printf("long: %ld\n", l);
    }
    void affidouble(va_list *p)
    {
        double f = va_arg(*p, double);
        printf("double: %f\n", f);
    }
    void affichar(va_list *p)
    {
        const char *s = va_arg(*p, const char *);
        printf("STRING: %s\n", s);
    }
     
    int lecture(char *format, ...)
    {
        void (*point1)(va_list *p);
        void (*point2)(va_list *p);
        void (*point3)(va_list *p);
        void (*point4)(va_list *p);
     
        va_list p_list;
     
        point1 = &affint;
        point2 = &affilong;
        point3 = &affidouble;
        point4 = &affichar;
     
        va_start(p_list, format);
        point1(&p_list);
        point2(&p_list);
        point3(&p_list);
        point4(&p_list);
     
        va_end(p_list);
     
        return 0;
    }
     
    int main(void)
    {
        lecture("NOT USED FOR THE MOMENT", 100, 200, 3.1415, "DVP.com");
        return EXIT_SUCCESS;
    }
    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  4. #4
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Points : 5 360
    Points
    5 360
    Par défaut
    Sur la base du dernier code, tu peux maintenant utiliser un tableau de pointeurs de fonction pour effectuer le traitement approprié:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
     
    enum Type {TYPE_INT, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_NB};
    typedef void (Fonction)(va_list *p);
     
    void affint(va_list *p)
    {
        int i = va_arg(*p, int);
        printf("int: %d\n", i);
    }
    void affilong(va_list *p)
    {
        long l = va_arg(*p, long);
        printf("long: %ld\n", l);
    }
    void affidouble(va_list *p)
    {
        double f = va_arg(*p, double);
        printf("double: %f\n", f);
    }
    void affichar(va_list *p)
    {
        const char *s = va_arg(*p, const char *);
        printf("STRING: %s\n", s);
    }
     
    int lecture(char *format, ...)
    {
        Fonction *point[] = {
            &affint,
            &affilong,
            &affidouble,
            &affichar
        };
     
        va_list p_list;
        va_start(p_list, format);
     
        point[TYPE_INT](&p_list);
        point[TYPE_LONG](&p_list);
        point[TYPE_DOUBLE](&p_list);
        point[TYPE_STRING](&p_list);
     
        va_end(p_list);
     
        return 0;
    }
     
    int main(void)
    {
        lecture("NOT USED FOR THE MOMENT", 100, 200, 3.1415, "DVP.com");
        return EXIT_SUCCESS;
    }
    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    75
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 75
    Points : 43
    Points
    43
    Par défaut analyse code
    Merci pour ton eclairage.

    Je tacherais de comprendre le code avant de l 'implementer et je ferais un effort pour les balises la prochaine fois.

    miguel

Discussions similaires

  1. Réponses: 10
    Dernier message: 03/02/2005, 13h09
  2. Réponses: 5
    Dernier message: 12/01/2005, 20h58
  3. pointeurs sur fonction en C++
    Par cemoi dans le forum C++
    Réponses: 7
    Dernier message: 29/11/2004, 13h19
  4. [langage] Pointeur sur fonction
    Par Fanch.g dans le forum Langage
    Réponses: 2
    Dernier message: 02/10/2004, 10h43
  5. Declaration de fonction retournant un pointeur sur fonction
    Par pseudokifaitladifférence dans le forum C
    Réponses: 5
    Dernier message: 11/08/2003, 19h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo