Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an alternate syntax to typedef function pointers?

For doing a typedef for a function pointer, we do something like this,

typedef int (*func) (char*);
typedef struct{
  char * name;
  func f1; 
}

As opposed to this, I came across a code, which I don't understand.

typedef int rl_icpfunc_t (char *);
typedef struct {
   char *name;         /* User printable name of the function. */
   rl_icpfunc_t *func; /* Function to call to do the job. */
   char *doc;          /* Documentation for this function.  */
}COMMAND;

This is a code snippet from an example of the libedit library. Can someone please explain this to me?

like image 426
Ankit Avatar asked Aug 18 '15 07:08

Ankit


2 Answers

typedef int rl_icpfunc_t (char *);

is defining a function prototype as type.

rl_icpfunc_t * func; 

defines func to be a pointer to the former.

This is as opposed to defining a function pointer type directly via:

typedef int (*prl_icpfunc_t) (char *);
prl_icpfunc_t func;

The result of both approches is the same: A pointer func, pointing to a function returning int and taking one argument, that is a char*.

like image 110
alk Avatar answered Nov 13 '22 09:11

alk


Is it correct to use typedef int rl_icpfunc_t (char *); ?

Yes that means rl_icpfunc_t is a function that takes a pointer to char and return and int. You can use rt_icpfunct_t in place of a normal type, thus rl_icpfunc_t *func means that func is a pointer to function of type rt_icpfunct_t

like image 41
artm Avatar answered Nov 13 '22 11:11

artm