我的 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/