I would like to know if it is possible to define a function type using typedef
, I tried this syntax:
typedef int (*) (void *, void *) order;
But it doesn't work.
Error message : expected identifier or '(' before ')' token
I would like to know if it is possible to define a function type using typedef
, I tried this syntax:
typedef int (*) (void *, void *) order;
But it doesn't work.
Error message : expected identifier or '(' before ')' token
The alias name should be placed where a variable name would be if it was a variable declaration.
Here:
typedef int (*order) (void *, void *);
// ^~~~~
Or, if you want a function type rather than a pointer-to-function:
typedef int order(void *, void *);
order foo; -> int foo(void*, void*);
. Or, if you add a star, function pointers: order *ptr;
(the pointer-to-function typedef does the same thing without the star). –
Emilyemina qsort
function. –
Burgess qsort
! –
Burgess int name(const void *, const void *)
. Then you wouldn't need a cast. –
Emilyemina int comp (const struct element *, const struct element *)
. And it worked well. I don't understand why it is a bad idea to do a cast? –
Burgess This typedef
typedef int (*order )(void *, void *);
defines an alias for the type of pointer to function of the type int( void *, void * )
.
This typedef
typedef int order(void *, void *);
defines an alias for the type of function of the type int( void *, void * )
.
If to use one typedef to define both the alias for pointer and the alias for function you can write
typedef int ( *porder )( void *, void * ), order(void *, void *);
or even the following way
int typedef ( *porder )( void *, void * ), order(void *, void *);
Pay attention to that you can use either typedef name as a type specifier for a function parameter. For example these two declarations
void h( porder arg );
void h( order arg );
declare the same one function because the compiler adjusts implicitly the type of a parameter having a function type to pointer to the function type.
However you may not use interchangeably these typedef(s) for the function return type because functions may not return functions but they may return pointers to functions. So this declaration
porder h( void );
will compile. But this declaration
order h( void );
will not compile.
Without the typedef the declaration of a function that returns pointer to other function would be complicated. For example this declaration
porder h( void );
without the typedef looks like
int ( *h( void ) )( void *, void * );
int* x, (*y)(int), z;
. –
Flosser You do either of
typedef int (*order) (void *, void *);
typedef int order(void *, void *);
depending on if you want to typedef the function or the pointer.
Both has advantages and disadvantages; I prefer a style where a pointer is visible as such and have my functions defined as
order order_impl;
int order_impl(void * a, void * b)
{
}
order * order_pointer = order_impl;
This ensures that the function is defined properly.
Others, however, prefer working with the function pointer type and do
porder order_pointer = order_impl;
and live without the said safeguard, leaving them with only a warning instead of an error in the case of a mismatch.
You declare a function pointer just like a function, except you wrap the function name in parenthesis and write a *
in front of the name.
void func (void); // function
void (*func) (void); // pointer to function
typedef void (*func) (void); // pointer to function type
However, my recommended style is one that doesn't hide pointers behind a typedef
:
typedef int order_t (void*, void*);
...
order_t* order;
The typedef
is a function template and the declaration is a function pointer declaration of that type. This makes function pointer syntax consistent with object pointers.
© 2022 - 2024 — McMap. All rights reserved.