• Welcome to Dark Bit Factory & Gravity.
 

function pointers? (i think)

Started by Stonemonkey, April 15, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Stonemonkey

Hi all, say i had something like:

typedef struct
{
  whatever operation;
}my_struct;

int add(int a,int b)
{
  return a+b;
}

int subtract(int a,int b)
{
  return a-b;
}


and to call it i want to do something like:


//test.operation=either add or subtract;
result=test.operation(a,b);

How would i go about something like that?

Cheers, Fryer.

Emil_halim



typedef int (CALLBACK *Function)(int,int);

Function myfunc;

int addFunc(int a,int b)
{
  return a+b;
}

int subtract(int a,int b)
{
  return a-b;
}

//test.operation=either add or subtract;
if(operation == add)
   myfunc = addFunc;
else
   myfunc = subtract;   
   
// call our desired one
result=myfunc(a,b);   

Stonemonkey