Connecting Tech Pros Worldwide Forums | Help | Site Map

Reg Fucntion pointers

Newbie
 
Join Date: Jun 2007
Posts: 3
#1: Jun 22 '07
Hi All,

I'm learning Pointers to functions in C.But I have some doubts regardign the same.I would be v.happy and thankful if somebody can help me out on my pbm
The problem is mentioned below:
------------------------------------------
Consider I have 5 different functions which returns different type and takes different arguements also.
Eg :
int aaa(int x);
void bbb(int x,int y, int z);
void ccc(void);
void ddd(char *x,char *y,char *z);
void eee(UINT8 x);

And these are all some operations when the user presses some commands,
Eg:
Commands = "command1","command2","command3","command4","comma nd5"

How will i use the fucntion pointers to execute the corresponding function with their arguements?

If possible, if somebody can reply to me ASAP.

Thanks in advance,
Sowmi

Member
 
Join Date: Mar 2007
Posts: 63
#2: Jun 22 '07

re: Reg Fucntion pointers


i didnt get "pressing commands "
can u explain it little more clearly
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#3: Jun 22 '07

re: Reg Fucntion pointers


Quote:

Originally Posted by sowmi

int aaa(int x);
void bbb(int x,int y, int z);
void ccc(void);
void ddd(char *x,char *y,char *z);
void eee(UINT8 x);

These functions will need different function pointers.
Expand|Select|Wrap|Line Numbers
  1. int aaa(int x);\
  2.  
will need a function pointer:
Expand|Select|Wrap|Line Numbers
  1. int (*fp)(int);
  2.  
You assign the address of the functtion to the pointer:
Expand|Select|Wrap|Line Numbers
  1. fp = aaa;
  2.  
and then you can call the function using the pointer:
Expand|Select|Wrap|Line Numbers
  1. fp(20);
  2.  
But you need a diffrerent function pointer for:
Expand|Select|Wrap|Line Numbers
  1. void bbb(int x,int y, int z);
  2.  
  3. void (*fp1)(int, int, int);
  4.  
  5. fp1 = bbb;
  6.  
  7. fp1(10,20,30);
  8.  
Your example is not a case where you use function pointers. Function pointers are used where you have many functions with the same arguments and the same return type. Then you can choose which function to call by putting its address in a function pointer. The pointer can be passed to another function so that the other function can call different functions. This is commonly done with sorts where you pass the address of the compare function to the sort so you can sort in different sequences.
Reply


Similar C / C++ bytes