c - 使用函数指针

标签 c pointers function-pointers

我的 C 函数有一个导致我的代码崩溃的函数,我很难弄清楚发生了什么。我有一个如下所示的函数:

#define cond int
void Enqueue(cond (*cond_func)());

cond read() {
return IsEmpty(some_global); // Returns a 1 or a 0 as an int
}

Enqueue(&read);

但是,当运行上面的代码时,一旦调用 Enqueue 就会出现段错误。它甚至不执行函数内部的任何内容。我运行了 gdb,它只是显示它在调用 Enqueue 后立即死亡 - 其中没有处理任何语句。知道发生了什么事吗?任何帮助将不胜感激。

最佳答案

您能否提供有关代码的更多信息,因为根据我的解释,代码工作正常。我已经尝试过 -

#define cond int
void Enqueue(cond (*cond_func)());
cond read() 
{
int some_global=1;
return IsEmpty(some_global); // Returns a 1 or a 0 as an int
}

int IsEmpty()
{
return 1;
}

void Enqueue(cond (*cond_func)())
{
printf("Perfect");
return 0;
}

int main()
{
Enqueue(&read);
return 0;
}

并且运行良好。

关于c - 使用函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19201487/

相关文章:

c - C语言中的栈,如何实现

c++ - 是否可以在定义函数的位置之外声明函数的属性? (海湾合作委员会)

c++ - vector 成员被重置且不可访问

c++ - typedef + 指向成员函数的指针

c++ - 向 MPI 进程发送函数

c++ - 通过套接字进行数据传输[TCP]如何在c/c++中打包多个整数并使用send()recv()传输数据?

c - 弹性/Bison : Error recovery destructors?

c++ - 为什么*ptr在printf中?

C: 不能用 void* 类型的右值初始化变量

c - 如何调用指向 typedef 结构中定义的函数的指针