简体   繁体   English

C中typedef结构中的调用函数

[英]call function in typedef struct in C

I found a similiar answer to my problem here . 我在这里找到了类似的答案。 But it is not working the way I expected. 但这并没有达到我的预期。 So I have 所以我有

void funcA(void) {
  // do sth.
}
void funcB(void) {
  // do sth.
}

typedef struct tasks {
    int val;
    void (*Start)(void);
} tasks; 

and

const tasks tasklist[] = 
    {       
        {0, funcA},
        {3, funcB}
    };

for (i=0; i < task_cnt; i++ )     
    if (tasklist[i].val == 3)
        tasklist[i]->Start();

But at "...->Start();" 但是在“ ...-> Start();”处 compiler says "expression must have pointer type". 编译器说“表达式必须具有指针类型”。

Any ideas? 有任何想法吗? Thanks 谢谢

you have to use tasklist[i].Start( ) instead of tasklist[i]->Start() 您必须使用tasklist[i].Start( )而不是tasklist[i]->Start()

this is due to the fact that ab is used for accessing member b of object a while a->b access a member b of object pointed to by a. 这是由于以下事实: ab用于访问对象a的成员b,而a-> b访问a指向的对象的成员b。

you can have the full explanation here 你可以在这里有完整的解释

您以与访问val相同的方式访问Start但带有一个点: tasklist[i].Start()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM