| 12
 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
 
 |  
#include <iostream>
#include <string>
#include <cstdarg>
#include <csignal>
#include <unistd.h>
#include <cstring>
 
void tracer(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
 
    while (*fmt != '\0')  {
        if (*fmt == 'd') {
            int i = va_arg(args, int);
            std::cout << i << " ";
        } else if (*fmt == 's') {
            char * s = va_arg(args, char*);
        std::cout << s << " ";           
        }
        ++fmt;
    }
    std::cout << std::endl;
 
    va_end(args);
}
 
int main()
{
    std::string str1( "Hello" ), str2( "world" );
 
    int k=10;
 
    // cas 1   
    //tracer("sds", str1.c_str(), 1, str2.c_str());  // OK 
    // cas 2
    //tracer("sds", str1.c_str(), k, str2.c_str(), 2, "ça marche");  // OK avec plus d'arguments que nécessaire
    // cas 3
    //tracer("sdsd", str1.c_str(), 3, str2.c_str(), "pas de SEGFAULT mais pas de sens");  // OK "pas de SEGFAULT mais pas de sens" affiche 4198152
    // cas 4
    //tracer("sdsdd", str1.c_str(), k, str2.c_str(), 5);  // OK la valeur de l'entier du dernier argument est aléatoire 
    // cas 5
    //tracer("sdss", str1.c_str(), k, str2.c_str(), 4);  // SEGFAULT : 4 n'est pas une chaîne  
    // cas 6
    tracer("sdsds", str1.c_str(), k, str2.c_str(), 5);  // SEGFAULT : manque une chaine dans les paramètres
 
    return 0 ;
} | 
Partager