c - 使用带有 va_arg 的自定义类型

标签 c macros variadic

mystruct_t v = va_arg(a_list, mystruct_t);

就 C 规范而言,这可以吗(使用大于等于 int 大小的自定义数据类型)?

最佳答案

C 标准中没有关于将结构类型用作可变参数的限制。所以你想做的事是允许的。

唯一不允许作为可变参数的参数类型是那些会进行提升的参数类型,即小于 int 的整数类型(signed 或 unsigned char short 或等效项)以及 float

作为引用,C standard 的第 7.16.1.1p2 节关于 va_arg 状态:

The parameter type shall be a type name specified such that the type of a pointer to an object that has the specified type can be obtained simply by postfixing a * to type. If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined

关于c - 使用带有 va_arg 的自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74857961/

相关文章:

c++ - 为什么C++全局变量不影响程序的内存使用

objective-c - NSLocalizedString 的宏

c++ - 如何编写丢弃其参数的通用可变参数 lambda?

c - 如何编写 C 函数接受(一个)任何类型的参数

c++ - 可变参数模板类的可变参数模板

c - 定义可以访问位、半字节、字节的 union

无法存储包含指针的结构

c - 在多个文件中重用 C 宏

C 函数,如宏 : expected an expression

找不到 n-gram 算法的泄漏