I want to create a function pointer array that holds an array of pointers to functions. But I keep getting the initialization from incompatible pointer type error at the fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
initializer.
I tried typedef void (*fun_ptr) (myStruct**,char**);
but the error still persists and I'm not sure what went wrong.
struct myStruct
{
char *value;
struct myStruct *next;
}
typedef void (*fun_ptr) (myStruct**,char*[]);
void fun1 (myStruct **theStruct, const char **arguments) {};
void fun2 (myStruct **theStruct, const char **arguments) {};
void fun3 (myStruct **theStruct, const char **arguments) {};
void main()
{
fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
}
And my error, for each of the functions
test.c:285:69: note: (near initialization for ‘fun_ptr_arr[0]’)
test.c:285:69: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
^~~~~~~~
Your prototype is typedef void (*fun_ptr) (myStruct**,char*[]);
Your functions are: void fun1 (myStruct **, const char **)
Note the extra const
Those are different types, and so, the compiler complains.
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.