Mel*_*oun 5 c parameters struct function-pointers type-conversion
今天再次重新输入..
在结构中是指向函数的指针,在这个函数中我希望能够处理来自这个结构的数据,所以指向结构的指针作为参数给出.
演示这个问题
#include <stdio.h>
#include <stdlib.h>
struct tMYSTRUCTURE;
typedef struct{
int myint;
void (* pCallback)(struct tMYSTRUCTURE *mystructure);
}tMYSTRUCTURE;
void hello(struct tMYSTRUCTURE *mystructure){
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
}
int main(void) {
tMYSTRUCTURE mystruct;
mystruct.pCallback = hello;
mystruct.pCallback(&mystruct);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
但是我得警告了
..\src\retyping.c:31:5:警告:从不兼容的指针类型传递'mystruct.pCallback'的参数1 ..\src\retyping.c:31:5:注意:预期'struct tMYSTRUCTURE*'但是参数的类型为'struct tMYSTRUCTURE*'
预期'struct tMYSTRUCTURE*'但是'struct tMYSTRUCTURE*',很有趣!
任何想法如何解决它?
问题是由typedef
结构引起的,然后使用struct
关键字和typedef
'd名称.转发声明struct
和typedef
解决问题.
#include <stdio.h>
#include <stdlib.h>
struct tagMYSTRUCTURE;
typedef struct tagMYSTRUCTURE tMYSTRUCTURE;
struct tagMYSTRUCTURE {
int myint;
void (* pCallback)(tMYSTRUCTURE *mystructure);
};
void hello(tMYSTRUCTURE *mystructure){
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
}
int main(void) {
tMYSTRUCTURE mystruct;
mystruct.pCallback = hello;
mystruct.pCallback(&mystruct);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)