[英]C how to get argument of type pointer to function from va_arg
I know that if I passed an argument like void (*func)(void *)
to a variadic function, I can retrieve the argument like: 我知道如果将像
void (*func)(void *)
这样的参数传递给可变参数函数,我可以像这样检索该参数:
void (*func)(void *) = va_arg( args, void (*)(void) );
What if I pass something like void (** func)(void *)
? 如果我传递了
void (** func)(void *)
怎么办? What is the correct syntax to retrieve an argument of this type using va_arg
? 使用
va_arg
检索此类型的参数的正确语法是什么?
Being frankly, your code is not standard-compliant. 坦率地说,您的代码不符合标准。 There is a tiny restriction for second argument of
va_arg()
macro: 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....参数
type
应该是指定的类型名称,以便可以通过后缀*来简单地获得指向具有指定类型的对象的指针的类型。 ......
According to this, notation like void (*)(void *)
is unacceptable in this case. 因此,在这种情况下,像
void (*)(void *)
这样的符号是不可接受的。 Since simple appending of *
won't give you pointer to pointer to function. 由于简单的
*
附加不会为您提供指向函数的指针。 You may use only typedef
-ed aliases: 您只能使用
typedef
别名:
typedef void (*func_ptr)(void *);
typedef void (**ptr_to_func_ptr)(void *);
func_ptr var1 = va_arg(ap, func_ptr);
ptr_to_func_ptr var2 = va_arg(ap, ptr_to_func_ptr);
Same as you've mentioned: 与您提到的相同:
typedef void (** func_t)(void *);
func_t ppf;
va_list vl;
va_start(vl,n);
ppf = va_arg(vl, func_t);
...
To help pointer-to-function, I always use typedef
as follows: 为了帮助指向函数,我总是使用
typedef
,如下所示:
typedef void VoidFn(void *); // Function accepts void * and returns nothing.
That declares the prototype of the function as a typedef
, allowing the use of the typedef
elsewhere. 那将函数的原型声明为
typedef
,从而允许在其他地方使用typedef
。
That way if you have this function: 这样,如果您具有以下功能:
void SomeFn(void *) {...}
you can declare this pointer: 您可以声明此指针:
VoidFn *fnPtr = &SomeFn; // A pointer to such a function.
This then makes it easier to change the prototype independently of the pointer, helping more... sophisticated constructs: 然后,这使得更容易独立于指针而更改原型,从而有助于更多...复杂的构造:
typedef void *VoidPtrFn(void *); // Function takes a void *, returning a void *
void *SomeOtherFn(void *) { ... }
VoidPtrFn *otherFnPtr = &SomeOtherFn;
VoidPtrFn **otherFnPtrPtr = &otherFnPtr;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.