简体   繁体   English

具有相同参数类型的结构中的函数指针

[英]function pointer in a struct with same argument type

I am trying to make a struct which has a function pointer for a function that takes the same struct as an argument. 我正在尝试创建一个结构,该结构具有一个函数的函数指针,该函数使用与参数相同的结构。 I have this at the moment. 我现在有这个。

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(sharedData*);
};

I am getting errors like 我收到的错误就像

error: expected ')' before '*' token 错误:在'*'标记之前预期')'

and warnings like 和警告一样

 warning: no semicolon at end of struct or union 
 warning: useless storage class specifier in empty declaration

what am I doing wrong here? 我在这做错了什么?

The problem is that when you're using typedef struct to introduce a new struct that doesn't require the struct keyword, you cannot refer to the typedef -ed name inside the declaration of the struct . 问题是当你使用typedef struct来引入一个不需要struct关键字的新struct ,你不能在struct的声明中引用typedef -ed名称。 Instead, you need to use the full name for the struct. 相反,您需要使用结构的全名。 For example: 例如:

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(struct sharedData*);
};

Also, your typedef statement is currently invalid because you haven't given a name by which to call struct sharedData . 此外,您的typedef语句当前无效,因为您没有给出用于调用struct sharedData的名称。 One way to fix this would be as follows: 解决这个问题的一种方法如下:

typedef struct sharedData
{
    sem_t* forks;
    int id;
    void (*forkFunc)(struct sharedData*);
} sharedData;

Now, you can refer to the struct by the full name struct sharedData or by the shorthand name sharedData . 现在,您可以通过全名struct sharedData或简写名称sharedData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将指针传递给结构作为函数参数 - Passing a pointer to a struct as a function argument Function 结构中的指针,将结构作为 C 中的参数 - Function pointer in struct, taking the struct as argument in C c结构将自身作为参数传递给指针函数 - c struct passing itself as an argument into a pointer function 将结构指针或数组作为函数参数传递 - Passing Struct Pointer or Array as Function Argument 无法在C中将结构指针作为函数参数传递 - Cannot pass struct pointer as function argument in C 以struct数组指针作为参数的函数,C语言 - function with a struct array pointer as an argument, C language 将指向具有 volatile 成员的结构的指针作为 function 参数传递 - Passing a pointer to a struct with a volatile member as a function argument 定义一个结构,其中包含一个函数指针,该函数指向一个以结构为参数的函数 - Define a struct that contains a function pointer to a function that takes the struct as an argument 将指向相同类型的结构成员的结构的指针分配给指向相同类型的结构的另一个指针 - Assign a pointer to a struct that is a member of a struct of the same type to another pointer to a struct of the same type Function 在 C 中带有指针类型返回和指针参数 - Function with Pointer type return and pointer argument in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM