C 中的函数指针和指针参数
对于 C
来说,完全是菜鸟,刚刚开始闲逛,想知道如何(阅读“if”)下面的内容是可能的。
尝试创建一个struct
,其成员是函数指针,并且该函数指针指向一个函数,该函数采用与上述struct
相同类型的参数。例如(注意语法,只是在这里熟悉一下):
typedef struct{
void (*myStructFunc)(void);
} MyStructType;
void myFunc(void){
printf("Hello world!");
}
// ...
MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(); // <= Hello world!
This 工作正常,但是当我尝试向函数引入 MyStructType
类型的参数时:
typedef struct{
void (*myStructFunc)(*MyStructType); // <= parse error
} MyStructType;
void myFunc(MyStructType *myStruct){
printf("Hello world!");
}
// ...
MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(&myStruct);
这些示例很简短原因显而易见,但它们说明了我的意图。再说一遍,我刚刚接触了C
,所以请原谅任何语法上的无知。
无论如何,我怎样才能实现这一目标?显然我在语法上做了一些不正确的事情,或者也许我正在尝试做一些明显不可能的事情。
另请注意,这样做的原因纯粹是学术性的。
Complete noob to C
, just getting started with some goofing around, wondering how (read "if") the following is possible.
Trying to create a struct
, with a member that is a function pointer, and the function pointer points to a function that takes an argument with the same type as the aforementioned struct
. For example (mind the syntax, just getting familiar here):
typedef struct{
void (*myStructFunc)(void);
} MyStructType;
void myFunc(void){
printf("Hello world!");
}
// ...
MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(); // <= Hello world!
This works fine, but when I try to introduce arguments of the MyStructType
type to the function:
typedef struct{
void (*myStructFunc)(*MyStructType); // <= parse error
} MyStructType;
void myFunc(MyStructType *myStruct){
printf("Hello world!");
}
// ...
MyStructType myStruct;
myStruct.myStructFunc = &myFunc;
myStruct.myStructFunc(&myStruct);
These examples are brief for obvious reasons, but they illustrate my intentions. Again, just getting my feet wet with C
so please forgive any syntactical ignorance.
Anyways, how can I achieve this? Clearly I'm doing something incorrect syntactically, or perhaps I'm trying to do something that's plain not possible.
Also note, that the reason for this is purely academic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在第二个示例中,在声明函数时未定义名称
MyStructType
。另外,您的*
位于错误的位置。您需要执行类似于以下操作的操作:In your second example, the name
MyStructType
isn't defined when you're declaring your function. Also, you have the*
in the wrong spot. You need to do something similar to the following:解析错误来自
*MyStructType
,这似乎是一个拼写错误,因为您在实际函数定义中正确声明了结构指针。修复该问题仍然是一个错误,因为在定义 struct 时,typedef 尚未生效。同样的问题可以用链表来说明(更清楚地恕我直言):
要解决它,您需要使用命名的
struct
:或者先放置
typedef
,然后定义(再次命名)struct
:或者(如果您感觉令人讨厌)只需使用
void *
指针:这同样适用于您的情况。
The parse error is coming from
*MyStructType
, which appears to be a typo as you've correctly declared the struct pointer in the actual function definition.Fixing that is still an error because, at the time of the
struct
definition, thetypedef
hasn't taken effect. The same problem can be illustrated (more clearly IMHO) with a linked list:To solve it, you need to use a named
struct
:Or put the
typedef
first, then define the (again named)struct
:Or (if you're feeling obnoxious) just use a
void *
pointer:The same applies to your situation.
尝试:
相反。在 C 中,结构体位于自己的命名空间中,因此如果要引用结构体,则需要在名称前添加 struct 前缀。您的
typedef
在您引用之后才可见,这是一个错误。如果你仍然想要 typedef,你可以这样做:Try:
instead. In C, structs are in their own namespace, so if you want to refer to a struct, you need to prefix the name by
struct
. Yourtypedef
isn't seen until after you reference, which is an error. If you still want the typedef, you can do: