0

I have some issue understanding some code in C. Here is an extract :

typedef struct player_s {
  point_t (*get_action)(struct player_s *, game_state_t *);
  void (*setup_boats)(struct player_s *, game_state_t *);
  char *name;
  point_t owned_rect[2];
  int n_boats;
} player_t;

I don't understand what do the 2 first line of this struct.. They also do this :

local_player_t *ret = calloc(1, sizeof(*ret));
ret->base.get_action = playerLocalAction;

local_player_t is a struct that contain a player_t.

and here is playerLocalAction prototype :

static point_t playerLocalAction(player_t *self, game_state_t *game);

I really don't understand what's appening in this code.. If you can help me thanks !

(Sorry for my bad english, I have to work on it x) )

1 Answers1

0

These are pointers to a function. They are defined as :

 [ret_type] (*[ptr_name])([arg_types])

Those function pointers are very useful in many situations, allowing a better modularity.

Magix
  • 4,989
  • 7
  • 26
  • 50