c - Typedef 函数语法

标签 c function variables typedef

我有以下 typedef 函数原型(prototype):

typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);

我不知道如何使用它。我知道 (int, siginfo_t *, void*)typedef 编辑到 sa_sigaction_t 中。但我将如何使用它?这些都是没有指定变量名的返回类型。

所以我假设我想创建一个变量:sa_sigaction_t handler。我将如何使用它?语法很困惑。

谢谢,

最佳答案

I understand that (int, siginfo_t , void) is typedef-ed into sa_sigaction_t.

其实没有。 sa_sigaction_t 是一个指向函数的指针,该函数返回 void 并以 (int, siginfo_t *, void *) 作为参数。

如果你有:

void foo(int, siginfo_t*, void*)
{
}

你可以这样做:

sa_sigaction_t fooPtr = &foo;

然后这样调用它:

fooPtr(0,NULL,NULL);

关于c - Typedef 函数语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701758/

相关文章:

javascript - 理解 React JS 中的 .env 文件和环境变量

可以打印但无法将字符串存储到 C 中的数组中

c - 搜索、const char 数组和指针运算

c++ - strincmp 的预期行为是什么?

javascript - 使用回调将两个数字相加和相乘

php - 将 array 中的参数传递给构造函数

c++ - 整数计数{0}。我可以在 C++ 中使用花括号初始化变量吗?

c - 创建 2 个 X11 窗口的简单 C 程序

c - bsearch() 按名称比较

PHP 如何保留链接中的所有 GET 变量?