C - 在另一个函数的参数中用作指针的 typedef 函数

C - the typedef function used as a pointer in the argument of another function

我有一个头文件定义了一些如下所示的代码:

typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
typedef void ChangeT(uint64_t post1, uint8_t post2);

struct ClusterT * ClientAlloc(EnrollT *, ChangeT *);

我有以下问题:

  1. 下面的代码是否相等?

    typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
    typedef uint8_t (*EnrollT)(uint16_t test1, uint16_t test2);
    
  2. 在包含这个头文件的C文件中,如何处理ClientAlloc函数中的这两个参数?示例代码对我来说很棒。

=========================================== ===========================

感谢您的回复。 通过有两个真正的功能,我将它们传递给以下代码:

ClientAlloc(Enroll, Change)

但是,当我编译代码时,出现以下错误,我在这里遗漏了什么吗?

expected declaration specifiers or ‘...’ before ‘Enroll’
expected declaration specifiers or ‘...’ before ‘NotifyChange’

解决第二个问题:

您必须使用 EnrollT 和 ChangeT 定义的签名创建两个函数(但您不能按名称使用类型 EnrollT 和 ChangeT):

uint8_t enroll(uint16_t test1, uint16_t test2){ ... };
void change(uint64_t post1, uint8_t post2){ ... };

然后将它们传递给函数调用:

ClientAlloc(enroll, change);

不,他们不是。

typedef uint8_t (*PEnrollT)(uint16_t test1, uint16_t test2);

定义一个与此签名匹配的函数指针类型。所以,

uint8_t EnrollT(uint16_t test1, uint16_t test2);

与此签名匹配 你可以像这样使用: PEnrollT pfn = EnrollT; 并用作 pfn(....); // 相当于调用 EnrollT

现在,你有

typedef uint8_t EnrollT(uint16_t test1, uint16_t test2);
typedef void ChangeT(uint64_t post1, uint8_t post2);

因此,您的类型 EnrollT 是“具有两个 uint16_t 参数返回 uinit8_t 的函数。 另外,你有,

struct ClusterT * ClientAlloc(EnrollT *, ChangeT *);

因此,如果您有两个匹配 EnrollT、ChangeT 的函数,您可以调用 ClientAlloc 传递这两个函数