views:

78

answers:

2

Take a function like printf that accepts a variable number of arguments what I would like to do is pass these variable number of functions to a sub function without changing their order. An example of this would be aliasing the printf function to a function called console ...

#include <stdio.h>

void console(const char *_sFormat, ...);

int main () {
    console("Hello World!");
    return 0;
}

void console(const char *_sFormat, ...) {
    printf("[APP] %s\n", _sFormat);
}

If I did for example console("Hello %s", sName), I would want the name to be passed to the printf function also, but it has to be able to continue to accept a varable number of arguments like the printf already does.

+2  A: 

There'll be another problem (noted by gf) -- you should probably concatenate the string in printf and the _sFormat parameter -- I doubt that printf is recursive -- hence the format statements in the first parameter wont be read!

Hence maybe such a solution would be nicer:

#include <stdarg.h>

void console(const char *_sFormat, ...)
{
  char buffer[256];

  va_list args;
  va_start (args, _sFormat);
  vsprintf (buffer,_sFormat, args);
  va_end (args);

  printf("[APP] %s\n", buffer);
}

Types/functions used:

Kornel Kisielewicz
1>.\file.cpp(43) : error C2065: 'c' : undeclared identifier1>.\file.cpp(43) : error C3861: 'va_start': identifier not found1>.\file.cpp(47) : error C3861: 'va_end': identifier not found
Mark Tomlin
@Mark, have you forgotten `#include <stdarg.h>` ?
Kornel Kisielewicz
Apparently, thank you very much for that. Still getting an undeclared identifier for 'c' tho. (Thank you for hand holding me through this.)
Mark Tomlin
printf() doesn't take a va_list.
Richard Pennington
@Richard, +1, completely forgot about it
Kornel Kisielewicz
It's good to also note the existence of the `vprintf` base function of `printf`. (See also Richard Thompson's answer: http://stackoverflow.com/questions/2206742/pass-a-variable-number-of-arguments-to-an-aliased-function/2206863#2206863 )
xtofl
@Kornel Kisielewicz, would there ever be a problem of the buffer overflowing should it go past 256 charters, or is that handled internally?
Mark Tomlin
@Mark - good point. Use vsnprintf if you're afraid of that, and pass the maximum value to the function.
Kornel Kisielewicz
+3  A: 

Here's what you want:

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

void console(const char *_sFormat, ...);

int main () {
    console("Hello World!");
    return 0;
}

void console(const char *_sFormat, ...) {
    va_list ap;
    va_start(ap, _sFormat);
    printf("[APP] ");
    vprintf(_sFormat, ap);
    printf("\n");
    va_end(ap);
}
Richard Pennington
You get a plus one for it compiling :).
Mark Tomlin
It also works: try console("Hello %s", "World!"); ;-)
Richard Pennington
I gave it too you sir, great answer, and it works fantasicly well and I can work with it for some other functions.@Kornel Kisielewicz, you where a great help. Thank you for your original answer also and the hand holding you did with the explanation of what everything did. Very useful.
Mark Tomlin