gpt4 book ai didi

c - va_list 是否必须是函数声明中的最后一个参数?

转载 作者:行者123 更新时间:2023-12-04 12:31:52 25 4
gpt4 key购买 nike

看似简单的问题,但一直未能在 SO 或 C 标准中找到答案。问题是 va_list 是否必须是 C 中函数的最后一个参数(当然,假设该函数将 va_list 作为参数)。

例如,这样安全吗?

int f(int a, char *b, va_list args, char *c);

还是必须是下面的

int f(int a, char *b, char *c, va_list args);

最佳答案

va_list 类型在用作函数参数方面没有什么特别之处,因为包含它并不会使它成为可变参数函数。这两个例子都是有效的。

一个实际的可变参数函数必须至少有一个命名参数和结尾的...,并且需要使用va_startva_end 设置一个 va_list 来读取可变参数。

C standard 的第 7.16p3 节描述 va_list 类型:

The type declared is

va_list

which is a complete object type suitable for holdinginformation needed by the macros va_start, va_arg, va_end,and va_copy. If access to the varying arguments is desired,the called function shall declare an object (generallyreferred to as ap in this subclause) having type va_list.The object ap may be passed as an argument to anotherfunction; if that function invokes the va_arg macro withparameter ap, the value of ap in the calling function isindeterminate and shall be passed to the va_end macro prior toany further reference to ap. 253)


  1. It is permitted to create a pointer to a va_list andpass that pointer to another function, in which case theoriginal function may make further use of the original list after theother function returns.

如此处所述,可以将 va_list 或指向 va_list 的指针传递给函数,但没有提及将其限制为最后一个参数。

关于c - va_list 是否必须是函数声明中的最后一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68534617/

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