我正在使用c中的xinu嵌入式操作系统.我创建了一个新的头文件并声明了一个结构:
struct callout {
uint32 time; /* Time of delay in ms */
void *funcaddr; /* Function pointer */
void *argp; /* Function arguments */
uint32 cid; /* Callout id for the specific callout */
char *sample;
Run Code Online (Sandbox Code Playgroud)
};
在我的主要部分中,我尝试声明一个struct对象并将funcaddr用于函数.
void test();
process main(void) {
struct callout *coptr;
coptr->sample ="hellowolrd";
coptr->funcaddr = &test;
(coptr->funcaddr)(coptr->argp); //error here
kprintf("coptr %s \n", coptr->sample);
return OK;
}
void test() {
kprintf("this is the test function \n");
}
Run Code Online (Sandbox Code Playgroud)
我尝试通过结构调用函数指针,但我收到一个错误:main.c:30:19:错误:被调用的对象不是函数或函数指针(coptr-> funcaddr)();
请说明调用函数指针的正确语法是什么.
您已声明funcaddr为对象指针.要声明一个函数指针,它看起来像这样:
struct callout {
uint32 time;
void (*funcaddr)(); // <-------- function pointer
Run Code Online (Sandbox Code Playgroud)
然后你的其余代码应该工作.
如果您没有看到该行的错误消息,coptr->funcaddr = &test;那么我建议您调整编译器设置,让编译器可以告诉您的信息非常重要.