Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does “typedef void *(*Something)(unsigned int)” mean

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);
like image 1000
Youshikyou Avatar asked Mar 19 '26 11:03

Youshikyou


1 Answers

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;
}
like image 167
vmt Avatar answered Mar 20 '26 23:03

vmt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!