I want to declare a pointer type which point to a function, so I try:
typedef void (*print)(void);  works perfect
void (*print)(void);   p is a ponter variable , not a type.                                                                                                                                                                       
typedef (void) (*print)(void); error expected identifier or ‘(’ before ‘void’ 
typedef void (*)(void) Print; 
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘_ attribute _’ before ‘Print’ .
My question is:
Do I have to use typedef to declare a function pointer type ?
Why typedef (void) (*print)(void); is wrong ? what () means here?
Why I can't write in this way:typedef void (*)(void) Print ?
The correct way is:
typedef void (*print_function_ptr)(void)
and its usage for variable/parameter declaration is:
print_function_ptr p;
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.
It does not mean anything it is not a valid C syntax.
Because it is not how C works. Typedefs in C mimics how variables are declared or defined.
No, you don't have to use a typedef to create an object of type 'pointer to function':
void somefunc(void (*pointer)(void))
{
    (*pointer)();
    pointer();
}
However, there is no way to create a name for a type other than by using a typedef.  I suppose you could indulge in macro hackery to generate a 'pointer to function', but after the macro is expanded, you'd have a 'pointer to function' written out:
#define PTR_FUNC(func) void (*func)(void)
void somefunc(PTR_FUNC(pointer)) { ... }
The (void) notation as the type name is wrong.  You don''t write: (int) x; and expect to declare a variable x -— it is a type cast.  Same with the notation you're using in your typedef.
You can't write typedef void (*)(void) Print; because it is not an allowed C syntax.  You also can't write typedef [32] int name; either — it isn't valid C syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With