What is the purpose of this?
typedef unsigned * (*item)(void* yyy);
What is the purpose of this?
typedef unsigned * (*item)(void* yyy);
cdecl: C gibberish <-> English
It's a function pointer.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
What is the difference then between the a bove to the ordinary way?
The ordinary way is: typedef void (Item_t)(int)
What can be the usage of this?
Do you have some context for the question?
> What can be the usage of this?
Typedef'ing a function pointer is very common.
Primarily because they're quite complicated to get right, and they get very verbose in a hurry.
So summarising a whole bunch of ( ) and * into a single word makes good sense.
Typedef'ing a function is basically unheard of.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Trying to explain the declaration... This:
Is a function named func which returns an unsigned int and takes a pointer to void as argument, right?Code:unsigned int func( void *p );
This:
Is the same, but returns a pointer to unsigned int. So, this other one is a pointer to a function which returns a pointer to unsigned int and takes a pointer to void as argument:Code:unsigned int *func(void *p);
Now we can transform this declaration in a typedef:Code:unsigned int *(*funcptr)(void *);
And use the type funcptr_T, instead of that complicated declaration, for example:Code:typedef unsigned int *(*funcptr_T)(void *);Code:typedef unsigned int *(*funcptr_T)(void *); unsigned int *f( void *p ) { ... do something... } unsigned int *g( void *p ) { ... do something else ... } unsigned int *example( unsigned int n, void *arg ) { // table with pointers to functions. const static funcptr_T fptr[] = { f, g }; if ( n < 2 ) return fptr[n](arg); // indirect call through a pointer. return NULL; }
Thanks flp1969, this is helpful