Kimi Heinonen
Kimi Heinonen

Reputation: 11

How to use a function pointer that is a member of a structure

I have a structure that has a function as one of it's members. Like

    Creature.move = moveAway;

moveAway being name of the function. So how can you exactly use that function?

Upvotes: 0

Views: 44

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477570

The member is a function pointer, and you can call it just like a normal function:

Creature.move();

Or, if it takes further arguments:

Creature.move(arg1, arg2, arg3);

(You can dereference the function pointer first if you like, but it'll just decay right back to a function pointer: (*Creature.move)();, (**Creature.move)();, (*****Creature.move)();, ...)

Upvotes: 3

AnT stands with Russia
AnT stands with Russia

Reputation: 320747

C does not have member functions, which means that your member apparently is not a function, but a function pointer. In order to call the target function through that pointer it you have a choice of either

Creature.move( /* arguments */ );

or

(*Creature.move)( /* arguments */ );

Choose whichever you like best. Both variants do the same thing.

Upvotes: 3

Related Questions