Tim
Tim

Reputation: 3

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

Upvotes: 0

Views: 1788

Answers (2)

Stefano
Stefano

Reputation: 4031

you have to use tasklist[i].Start() instead of tasklist[i]->Start()

this is due to the fact that a.b 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

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

You access Start the same way you access val — with a dot: tasklist[i].Start().

Upvotes: 1

Related Questions