c - Variadic functions problems -


let's consider such code:

#include <stdio.h> #include <stdarg.h>  #ifdef debug #undef debug #endif  #define debug(format, ...) tpk(format, __va_args__)  void tpk(const char* format, ...) {   const unsigned int len = 1024;   char buffer[len];   va_list args;    va_start(args, format);   vsprintf(buffer, format, args);   va_end(args);    printf(buffer); }   int main() {   debug("no, don't! ",  "but do! %d %s\n", 34, "blabla");   return 0; } 

things consider:

i have code debug, that's why have undef debug (please don't ask me why, because way headers included in project kind of messed up).

don't worry buffer overflows , such, debugging purposes.

what's not working:

i no, don't! message , that's all. however, if remove first argument debug message printed nicely. doing wrong here?

update: made stupid mistake. give format string, has no formatting parametres, hence works should. solution this, modify function:

#define debug(str, format, ...) tpk(str, format, __va_args__) 

print str , after use variadic functions on format.

what you're saying no different from:

printf("no, don't! ",  "but do! %d %s\n", 34, "blabla"); 

can see does?

hint: what's format string?


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -