Search code examples
cvariadic-functionsc89cvi

How can i pass a va_list through a function in C90


I want to pass a va_list through to another function. Here is an example of what i am trying to do:

void my_printf_1(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, ap);
    va_end(ap);
}

void my_printf_2(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, *ap);
    va_end(ap);
}

int main(void) {
    my_printf_1("Hello %i\n", 12); //Shows me the pointer
    my_printf_1("Hello \n"); //Runtime Error - too many arguments
    my_printf_2("Hello %i\n", 12); //Displays correctly because 12 < char
    my_printf_2("Hello %i\n", 500); //Displays incorrectly  because 500 > char
    my_printf_2("Hello %i %i\n", 12, 12); //Displays 12, then crashes Runtime Error not enough arguments
    my_printf_2("Hello \n"); //Runtime Error - too Many Arguments
}

It seems to me that my va_list ap is a char pointer, how can i get the whole List? How can i rewrite my_printf() to pass the whole or a fake va_list to subfunctions? I can not modify my subfunctions to accept va_list pointers, so i will have to modify my_printf.

EDIT: I realize that the following would work, however this is not what i want.

void my_printf_1(char* string, ...) {
    va_list ap;
    va_start (ap, string);
    vprintf(string, ap);
    va_end(ap);
}

Solution

  • You need to do what you say, but explicitly. That is, pass the actual va_list object.

    See the vsnprintf() function for inspiration. Functions that take an explicit va_list are often prefixed with a v in the standard library.

    The va_list doesn't have an internal structure that you can work with, it's opaque. All you can do is defined in the <stdarg.h> header.