I am wondering, what does below mean in.h, it has
typedef void *(*some_name)(unsigned int);
And then in.c
some_name rt;
some_name State= 0;
unsigned int t = 1;
rt = (some_name) State(t);
It creates an alias some_name
for a pointer to a function with a return type void*
and a single unsigned int parameter. An example:
typedef void *(*my_alloc_type)(unsigned int);
void *my_alloc(unsigned int size)
{
return malloc(size);
}
int main(int argc, char *argv[])
{
my_alloc_type allocator = my_alloc;
void *p = allocator(100);
free(p);
return 0;
}
This typedef
typedef void *(*some_name)(unsigned int);
introduces an alias for pointer to a function of the type void *( unsigned int )
that is that has the return type void *
and one parameter of the type unsigned int
.
As for this code snippet
some_name rt;
some_name State= 0;
unsigned int t = 1;
rt = (some_name) State(t);
then it does not make a sense.
typedef void *(*)(unsigned int); ^^^^^^^^^
somename
is
typedef void *some_name(unsigned int); ^^ ^
a pointer
typedef void *(*some_name); ^^^^^^^^^^^^^^
to a function that accepts an unsigned int
typedef(*some_name)(unsigned int); ^^^^^^
and returns a pointer to void
void *(*some_name)(unsigned int);^^^^^^^
but for types.
So "the type somename
is for functions that accept unsigned and return pointer to void"
#include <stdlib.h>
typedef void *(*some_name)(unsigned int);
void *foo(unsigned t) {
if (t == 0) return NULL;
return malloc(t);
}
int main(void) {
somename fxptr = foo;
void *bar = fxptr(42);
free(bar);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.