How can I call a function using a function pointer?

How do I call one of these functions conditionally using a function pointer, and how do I declare the function pointer?

31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges asked Dec 23, 2009 at 11:15 Priya Priya

17 Answers 17

You can do the following: Suppose you have your A,B & C function as the following:

bool A() < . >bool B() < . >bool C()

Now at some other function, say at main:

int main() < bool (*choice) (); // now if there is if-else statement for making "choice" to // point at a particular function then proceed as following if ( x == 1 ) choice = A; else if ( x == 2 ) choice = B; else choice = C; if(choice()) printf("Success\n"); else printf("Failure\n"); . . >

Remember this is one example for function pointer. there are several other method and for which you have to learn function pointer clearly.

22.1k 9 9 gold badges 77 77 silver badges 116 116 bronze badges answered Dec 23, 2009 at 11:27 6,577 10 10 gold badges 37 37 silver badges 54 54 bronze badges

There are some examples in this link. You can refer this link cprogramming.com/tutorial/function-pointers.html/… Also u can visit other link like publications.gbdirect.co.uk/c_book/chapter5/… etc.

Commented Dec 23, 2009 at 11:39

I think your question has already been answered more than adequately, but it might be useful to point out explicitly that given a function pointer

void (*pf)(int foo, int bar); 
pf(1, 0); (*pf)(1, 0); 

are exactly equivalent in every way by definition. The choice of which to use is up to you, although it's a good idea to be consistent. For a long time, I preferred (*pf)(1, 0) because it seemed to me that it better reflected the type of pf , however in the last few years I've switched to pf(1, 0) .

answered Dec 23, 2009 at 12:13 Dale Hagglund Dale Hagglund 16.4k 4 4 gold badges 32 32 silver badges 37 37 bronze badges I think so too, but can you share an official reference stating the two forms are identical? Commented Sep 15, 2016 at 8:47

@lkanab §6.5.2.2.1 and it's footnote 94, along with the definition of function designator at §6.3.2.1.4 show that (surprisingly, to me) pf(1, 0); is "converted" to (*pf)(1, 0); (sorry if my numbering is wrong)

Commented Sep 3, 2018 at 22:18

@pizzapants184 Thanks for adding the references. It might be helpful to point out that function and array types both share the property of automatically decaying to a pointer in most contexts. So, without having a C compiler to hand to try it out, I believe you'll find, assuming we have a function pointer fp already initialized to point to func , that func == fp and *fp == fp .

Commented Sep 3, 2018 at 22:29

@Sekomer The basic rule that explains both your questions is that any time a function occurs in an expression would result in a function object, the expression is forced to be instead a pointer to the function. This is often described by saying that functions decay into pointers. Re (1) the first form is the only "real" way to call a function. In the second form, *pf would normally evaluate to a function object since you're dereferencing a pointer, but the decay rule the kicks in and you get a pointer back, and then the first form applies. Exactly the same thing happens in printf(. ) .

Commented Dec 18, 2020 at 6:48

@Sekomer Just to finish up, re (2) the same decay rule explains what's happening here. And, in closing, I'll note that the same decay rule applies to arrays: an array object in an expression is always converted to a pointer to the first element.

Commented Dec 18, 2020 at 6:49

Declare your function pointer like this:

bool (*f)(); f = A; f(); 
answered Dec 23, 2009 at 11:20 Pablo Santa Cruz Pablo Santa Cruz 180k 33 33 gold badges 247 247 silver badges 298 298 bronze badges

Initially define a function pointer array which takes a void and returns a void.

Assuming that your function is taking a void and returning a void.

typedef void (*func_ptr)(void); 

Now you can use this to create function pointer variables of such functions.

func_ptr array_of_fun_ptr[3]; 

Now store the address of your functions in the three variables.

array_of_fun_ptr[0]= &A; array_of_fun_ptr[1]= &B; array_of_fun_ptr[2]= &C; 

Now you can call these functions using function pointers as below:

some_a=(*(array_of_fun_ptr[0]))(); some_b=(*(array_of_fun_ptr[1]))(); some_c=(*(array_of_fun_ptr[2]))(); 
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Dec 23, 2009 at 11:38 66.8k 91 91 gold badges 234 234 silver badges 326 326 bronze badges Your array size is 2, so your index can only go up to 1. Commented Dec 23, 2009 at 16:37 @Vijay why are you passing void in the function calls? void is a type, not a value. Commented Feb 22, 2014 at 8:41

A few minor corrections: 1. No need for an & before the function. array_of_fun_ptr[0] = A is good enough. 2. The void in the function call is an error. 3. No need for an * before a function invocation. array_of_fun_ptr[0]() is much cleaner. No harm done, though :)

Commented Dec 22, 2014 at 16:23
bool (*FuncPtr)() FuncPtr = A; FuncPtr(); 

If you want to call one of those functions conditionally, you should consider using an array of function pointers. In this case you'd have 3 elements pointing to A, B, and C and you call one depending on the index to the array, such as funcArray0 for A.

answered Dec 23, 2009 at 11:18 Firas Assaad Firas Assaad 25.6k 16 16 gold badges 62 62 silver badges 79 79 bronze badges

You can declare the function pointer as follows:

bool (funptr*)(); 

Which says we are declaring a function pointer to a function which does not take anything and return a bool.

funptr = A; 

To call the function using the function pointer:

funptr(); 
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Dec 23, 2009 at 11:27 453k 83 83 gold badges 498 498 silver badges 534 534 bronze badges I think you meant bool (*funptr)(); , with the * on the left of the pointer. Can you please check? Commented Dec 17, 2019 at 8:38 Your declaration is invalid. It won't even compile. Commented Dec 31, 2020 at 12:46

Note that when you say:

bool (*a)(); 

you are declaring a of type "pointer to function returning bool and taking an unspecified number of parameters". Assuming bool is defined (maybe you're using C99 and have included stdbool.h , or it may be a typedef), this may or may not be what you want.

The problem here is that there is no way for the compiler to now check if a is assigned to a correct value. The same problem exists with your function declarations. A() , B() , and C() are all declared as functions "returning bool and taking an unspecified number of parameters".

To see the kind of problems that may have, let's write a program:

#include int test_zero(void) < return 42; >static int test_one(char *data) < return printf("%s\n", data); >int main(void) < /* a is of type "pointer to function returning int and taking unspecified number of parameters */ int (*a)(); /* b is of type "pointer to function returning int and taking no parameters */ int (*b)(void); /* This is OK */ a = test_zero; printf("a: %d\n", a()); a = test_one; /* OK, since compiler doesn't check the parameters */ printf("a: %d\n", a()); /* oops, wrong number of args */ /* This is OK too */ b = test_zero; printf("b: %d\n", b()); /* The compiler now does type checking, and sees that the assignment is wrong, so it can warn us */ b = test_one; printf("b: %d\n", b()); /* Wrong again */ return 0; >

When I compile the above with gcc, it says:

warning: assignment from incompatible pointer type

for the line b = test_one; , which is good. There is no warning for the corresponding assignment to a .

So, you should declare your functions as:

bool A(void); bool B(void); bool C(void); 

And then the variable to hold the function should be declared as:

bool (*choice)(void);