gpt4 book ai didi

c++ - 参数列表中的 C 函数声明

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:15 24 4
gpt4 key购买 nike

我在 C 代码中发现了一些令人困惑的东西

struct SomeStruct {
// ...
void (*f)(const void *x);
};

void do_some( void f(const void *x) ) { // what?
struct SomeStruct* v;
// ...
v->f = f;
}

据我所知,do_some 采用函数而不是函数指针。但是 void do_some( void (*f)(const void *x) ) 在实践中有什么区别?我应该什么时候使用它?这在 C++ 中允许吗?

最佳答案

没有区别。这只是语法糖。它在 C 和 C++ 中都是允许的。

函数参数只是被编译器重写为函数指针参数,就像数组参数被编译器重写为指针参数一样。

作为引用,这里是 C 标准第 3.7.1 节的摘录:

g(int (*funcp)(void))

// ...

or, equivalently,

g(int func(void))

关于c++ - 参数列表中的 C 函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29470474/

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