gpt4 book ai didi

c - 我将如何使用 `...` C 运算符来获得打印出所有参数的函数?

转载 作者:太空狗 更新时间:2023-10-29 15:01:24 26 4
gpt4 key购买 nike

这是我尝试过的……但失败了:

void testfunc(...){
printf(...);
}

最佳答案

这将创建一个等同于 printf 的函数。请注意,您不能盲目地打印出参数,因为您需要以某种方式提前知道每个参数的类型。 printf 的 format 参数通知它期望什么参数以及它们将是什么类型。

#include <stdargs.h>

void testfunc(const char *format, ...)
{
va_list ap;
va_start(ap, format);

vprintf(format, ap);

va_end(ap);
}

关于c - 我将如何使用 `...` C 运算符来获得打印出所有参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com