简体   繁体   中英

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();" compiler says "expression must have pointer type".

Any ideas? Thanks

you have to use tasklist[i].Start( ) instead of 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.

you can have the full explanation here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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