• # Une solution

    Posté par . En réponse au message Probleme de memoire, sprintf. Évalué à 1.

    Voilà, peut-être, une solution. Pas forcément très efficace en termes de temps, mais robuste.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    extern char* getipaddress(const char *) ;

    int main()~
    {
    char * myip;
    char * mystring;

    char cal_long[2]; // CHAINE BIDON POUR DETERMINER LA LONGUEUR A ECRIRE
    int longueur; // CONTIENT LA LONGUEUR DE LA CHAINE A ECRIRE

    myip = getipaddress("eth0");
    longueur = snprintf(mystring,sizeof(cal_long),"mon adresse ip est :%s",myip); // CALCUL DE LA LONGUEUR
    if ((mystring = malloc(longueur)) == NULL)
    {printf("ERREUR ALLOCATION MEMOIRE\n");
    exit(1);
    }
    sprintf(mystring,"mon adresse ip est :%s",myip);
    // printf("ici mon string : %s",mystring);
    free(mystring);
    return(1);
    }