【发布时间】:2022-10-18 22:15:10
【问题描述】:
使用 <stdarg.h> 标头,可以创建一个具有可变数量参数的函数,但是:
-
要开始使用
va_list,您需要使用需要知道那里有多少参数的va_start宏,但是使用va_list的printf& ... 不需要参数计数。如何创建一个不需要像printf这样的参数计数的函数? -
假设我想创建一个接受
va_list& 而不是使用它的函数,将它传递给另一个需要va_list的函数? (所以在伪代码中就像void printfRipOff(const char* format, ...) {printf(format, ...);})
【问题讨论】:
-
va_start macro that needs to know how many arguments thereva_start宏不需要知道有多少参数。How can I create a function that doesn't need the argument count like printf?printf“知道”参数计数 - 它计算格式字符串中%的数量,而不是另一个%。say I want to create a function that takes a va_list & instead of using it, passes it to another function that requires a va_list?这么说吧,然后呢?所以写那个函数。 -
sprintf和printf是相同的,除了printf打印到stdout和sprintf打印到作为第一个参数传递的char* -
另请参阅C FAQ list 中的question 15.4。 Question 15.5 和 section 15 的其余部分也可能对您感兴趣。
-
如果你通缉由也提供答案的人提供,您应该考虑单击空心复选标记以表明您已接受该答案。我还建议单击您认为有帮助的任何其他答案。
-
与其在问题中加上“谢谢”,不如accept an answer。
标签: c standard-library