Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a pointer variable to the function in C program

If I want to define a pointer variable p to point to the function foo() defined as below, what should be the exact type of p?

int *foo(void *arg)
{
    ...
}
like image 945
Kamie Xeng Avatar asked Apr 17 '26 15:04

Kamie Xeng


2 Answers

It should be

typedef int *(*funtion_foo_type)(void *);
like image 88
Iharob Al Asimi Avatar answered Apr 19 '26 05:04

Iharob Al Asimi


You need to have the pointer as the pointer to a function, returning an int *, accepting a void* argument. You can make it like

int * (*p) (void *);

and then, you can use

p = foo;
like image 33
Sourav Ghosh Avatar answered Apr 19 '26 03:04

Sourav Ghosh



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!