Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to Function Pointer

Is it possible to create a pointer to a function pointer, i.e.

int32_t (*fp[2])(void) = {test_function1, test_function_2}; // initialize a function pointer

<unknown> = fp;

What needs to be written in place of unknown? With "normal" arrays, I could do this:

int a[2] = {0, 1};

int* p = a;

Many thanks in advance.

like image 237
user2389717 Avatar asked Sep 24 '14 09:09

user2389717


People also ask

How do you use a function pointer in C?

Function Pointer in C. In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer. #include <stdio.h>. // A normal function with an int parameter. // and void return type.

What is the difference between pointers and functions?

In lesson 10.8 -- Introduction to pointers, you learned that a pointer is a variable that holds the address of another variable. Function pointers are similar, except that instead of pointing to variables, they point to functions!

Is there a generic function pointer type called function?

Assuming for the moment that C (and C++) had a generic "function pointer" type called function, this might look like this: void create_button ( int x, int y, const char *text, function callback_func );

How to declare a function that returns a void pointer?

If we remove bracket, then the expression “void (*fun_ptr) (int)” becomes “void *fun_ptr (int)” which is declaration of a function that returns void pointer. See following post for details. How to declare a pointer to a function?


2 Answers

typedef void(*func_ptr_t)(void); // a function pointer

func_ptr_t* ptr_to_func_ptr;     // a pointer to a function pointer - easy to read
func_ptr_t  arr[2];              // an array of function pointers - easy to read

void(**func_ptr_ptr)(void);      // a pointer to a function pointer - hard to read
void(*func_ptr_arr [2])(void);   // an array of function pointers - hard to read
like image 73
Lundin Avatar answered Oct 08 '22 04:10

Lundin


typedef int32_t FP(void);

FP *fp[2] = { test_function1, test_function2 };
FP **p = fp;
like image 43
M.M Avatar answered Oct 08 '22 06:10

M.M