waka-waka-waka
waka-waka-waka

Reputation: 1045

c assign function pointer to variable

I have two functions,

//virDomain is some struct
int virDomainCreate(virDomain*);
int virDomainDestroy(virDomain*);

How do I assign these two functions to a variable?

I tried,

int (*func)(virDomain*) = NULL;
func = virDomainCreate(virDomain*); // not working
func = &virDomainDestroy(virDomain*); //not working

Thanks for all your help! Waka.

Upvotes: 2

Views: 10958

Answers (2)

learner
learner

Reputation: 4818

The return type is int so

int func;
func = virDomainCreate(virDomain*); 
func = virDomainDestroy(virDomain*);

will work.

Upvotes: 1

Arjun Sreedharan
Arjun Sreedharan

Reputation: 11453

You can just assign the pointer to the function like:

func = &virDomainCreate;

Or you can just use the short format:

func = virDomainCreate;

Upvotes: 7

Related Questions