我想将va_list传递到另一个功能。这是我要做的示例:

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
}

在我看来我的 va_list ap 是一个 char 指针,如何获得整个列表?我该如何重写 my_printf() 通过全部或假 va_list 到亚功能?我无法修改我的子功能以接受 va_list 指针,所以我必须修改 my_printf.

编辑:我意识到以下内容会起作用,但是这不是我想要的。

void my_printf_1(char* string, ...){
va_list   ap;
va_start (ap, string);
vprintf(string, ap);
va_end(ap);
}
有帮助吗?

解决方案

您需要做自己说的事情,但要明确。也就是说,通过实际 va_list 目的。

看到 vsnprintf() 灵感的功能。明确的功能 va_list 经常有一个前缀 v 在标准库中。

va_list 没有您可以使用的内部结构,它是不透明的。您所能做的就是在 u003Cstdarg.h> 标题。

其他提示

查看Vprintf和朋友。 http://www.manpagez.com/man/3/vprintf/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top