gpt4 book ai didi

c - 在c中传递可变数量的不同类型的参数

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:03 25 4
gpt4 key购买 nike

<分区>

我想创建一个函数,它接受不同类型的可变数量的参数。我的问题是如何解析参数类型。 Like 调用者可以按任何顺序传递任何类型的参数(无需指定格式说明符)。那么在调用函数中我如何区分类型。

如何打印函数 var_argument 中调用者传递的所有值?

假设我想在调用者传递的参数的 var_argument 函数中执行 sprintf

这是我的C代码

#include <stdio.h>
#include <stdarg.h>
void var_argument(int num,...);

int main()
{
var_argument(3,"Hello",7.87f,6);
var_argument(3,7.87f,"Hello",6);
return 0;
}

void var_argument(int num,...)
{
char str[80];
/* What code should I write to differentiate between types and to print all the values passed by the caller */

va_list list;
va_start(list,num);

/* How to do sprintf of the parameter passed to this function and store
the formatted output in the array str */
}

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