Pergunta

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);
}

void 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);
}
Foi útil?

Solução

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.

Outras dicas

Check out vprintf and friends. http://www.manpagez.com/man/3/vprintf/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top