Re: question de c

Pàgina inicial

Reply to this message
Autor: Edgar Bonet
Data:  
A: Liste Guilde
Assumpte: Re: question de c
Le mardi 30 mars, Patrick Dupre a écrit :
> Je souhaite definir un pointeur sur une fonction retournant un type
> defini, mais aceptant un nombre non prefini d'element de type non
> predefini ! Je sais c'est pas tres conseille, mais c'est une question
> d'efficacite (controm minimum).


Voici un Hello World un peu tordu :

    #include <stdio.h>
    #include <stdarg.h>


    typedef int (*print_function)(char *, ...);


    int my_print_function(char *fmt, ...)
    {
        va_list ap;
        int ret;


        printf("my_print_function: ");
        va_start(ap, fmt);
        ret = vprintf(fmt, ap);
        va_end(fmt);
        return ret;
    }


    print_function get_print_function(void)
    {
        return my_print_function;
    }


    int main(void)
    {
        print_function print;


        print = get_print_function();
        print("Hello, World!\n");
        return 0;
    }


-- 
Edgar Bonet           Maison : 04 76 21 29 16    Bureau : 04 76 88 10 96
3 rue Jean Prévost    Mobile : 06 77 19 79 39    Fax    : 04 76 88 11 91
38000 Grenoble        guilde@???     www.edgar-bonet.org