typedef a function pointer type

后端 未结 2 562
轻奢々
轻奢々 2021-01-03 08:14

I want to declare a pointer type which point to a function, so I try:

typedef void (*print)(void); works perfect

void (*print)(void);

2条回答
  •  半阙折子戏
    2021-01-03 08:54

    The correct way is:

    typedef void (*print_function_ptr)(void)
    

    and its usage for variable/parameter declaration is:

    print_function_ptr p;
    
    1. You don't need a typedef to declare a variable. You can directly write void (*p)(void) to declare a variable p pointing to a function taking void and returning void. However to declare a type alias / name for a pointer to function, typedef is the tool.

    2. It does not mean anything it is not a valid C syntax.

    3. Because it is not how C works. Typedefs in C mimics how variables are declared or defined.

提交回复
热议问题