views:

621

answers:

13

Suppose I have these three functions:

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

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

+3  A: 
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.

Firas Assaad
+7  A: 

Declare your function pointer like this:

bool (*f)();
f = A;
f();
Pablo Santa Cruz
`f*`? Of course you meant `*f` :-)
Alok
of course! thanks for pointing that out...
Pablo Santa Cruz
A: 
//Declare the pointer and asign it to the function
bool (*pFunc)() = A;
//Call the function A
pFunc();
//Call function B
pFunc = B;
pFunc();
//Call function C
pFunc = C;
pFunc();
Khelben
+4  A: 

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.

Next Assignment:

funptr = A;

To call the function using the function pointer:

funptr();
codaddict
+6  A: 

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;

  elseif ( 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 u have to learn function pointer clearly.

Abhi
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.
Abhi
A: 

I usually use typedef to do it, but it may be overkill, if you do not have to use the function pointer too often..

//assuming bool is available (where I come from it is an enum)

typedef bool (*pmyfun_t)();

pmyfun_t pMyFun;

pMyFun=A; //pMyFun=&A is actually same

pMyFun();
Sint
+1  A: 

You declare a function pointer variable for the given signature of your functions like this:

bool (* fnptr)();

you can assign it one of your functions:

fnptr = A;

and you can call it:

bool result = fnptr();

You might consider using typedefs to define a type for every distinct function signature you need. This will make the code easier to read and to maintain. i.e. for the signature of functions returning bool with no arguments this could be:

typdef bool (* BoolFn)();

and then you can use like this to declare the function pointer variable for this type:

BoolFn fnptr;
x4u
+1  A: 

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 u can use this to create function pointer variables of such functions. like below:

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 function using function pointers as below:

some_a=(*(array_of_fun_ptr[0]))(void);
some_b=(*(array_of_fun_ptr[1]))(void);
some_c=(*(array_of_fun_ptr[2]))(void);
Vijay Sarathi
Your array size is 2, so your index can only go up to 1.
Alok
thanks for correcting it:)
Vijay Sarathi
+2  A: 
bool (*fptr)();

int main(void)
{
  ...
  ...
  printf("Enter your choice");
  scanf("%d",&a);
  switch(a)
{
  case 0:
         fptr = A;
         break;
  case 1:
         fptr = B;
         break;
  case 2:
         fptr = C;
         break;
  case 3:
          break;
}
(*fptr)();
return 0;
}

your choice is stored in a; Then accordingly, functions are assigned in the function pointer. Finally depending on your choice, the very same function is called to return the desired result.

wrapperm
A: 

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);

the two calls

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).

Dale Hagglund
+1  A: 

Slightly different approach:

bool A() {...}
bool B() {...}
bool C() {...}

int main(void)
{
  /**
   * Declare an array of pointers to functions returning bool
   * and initialize with A, B, and C
   */
  bool (*farr[])() = {A, B, C};
  ...
  /**
   * Call A, B, or C based on the value of i
   * (assumes i is in range of array)
   */
  if (farr[i]()) // or (*farr[i])()
  {
    ...
  }
  ...
}
John Bode
A: 

This has been more than adequately answered, but you may find this useful: The Function Pointer Tutorials. It is a truly comprehensive treatment of the subject in five chapters!

Clifford
Handily accessible from: http://function-pointer.org/
Nicolás
A: 

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 <stdio.h>

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);
Alok