How to handle different functions using function pointers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to handle different functions using function pointers

2,177 Views
anunalge
Contributor I
In my program i need to handle different type of functions.I created some structure for accessing different functions.

typedefstruct{
UCHAR (*const GetFunc)(void
);
}
UCHAR_CPMTS;

typedef struct{
UINT (*const GetFunc)(void
);
}UINT_CPMTS;

typedefstruct{
void
(*constSetFunc)(UCHAR);
}
UCHAR_CPMTS_INIT;

typedef
struct{
void
(*constSetFunc)(UINT);
}UINT_CPMTS_INIT;

using this pointers i can handle only  functions having uchar,uint return types and as arguements.But i need to handle SCHAR, as well as functions with more no of arguements.How to handle them in generic way,Instead of creating one structure for every fuction type.Please help me.I need the solution very urgent.

Thanks,

Anuradha.

Labels (1)
0 Kudos
Reply
5 Replies

833 Views
bigmac
Specialist III
Hello Anuradha,
 
This post is really more applicable to the Code Warrior forum.
 
I cannot quite see what you are ultimately trying to achieve, and why it is necessary to create a structure containing a single function pointer.
 
Regards,
Mac
 
0 Kudos
Reply

833 Views
anunalge
Contributor I
I will create an array of structures,empty list,which can be configurable depend on the program.Like you can add functions to the list as per requirement .
0 Kudos
Reply

833 Views
bigmac
Specialist III
Hello Anuradha,
 
Would not the same result be achieved more directly using an array of function pointers?  The parameter for each function then might be a pointer to a common structure type, which could take into account the differing data types required by each function.
 
Regards,
Mac
0 Kudos
Reply

833 Views
anunalge
Contributor I
Hi Mac,
         As you said the result can be acheived by array of function pointers also.But i didn't get your next point.Can you explain it again.My problem is i need to handle different functions with variable return type and arguments.The prototype also depends on individual program.I am trying to make my code as reusable for other projects.
 


The parameter for each function then might be a pointer to a common structure type, which could take into account the differing data types required by each function.


Thanks,
Anu.
0 Kudos
Reply

833 Views
bigmac
Specialist III
Hello Anu,
 
My previous suggestion was to define a structure that would contain both parameter and return data of various types used by the different functions.  For example -
 
typedef struct {
   char returnval1;
   int  returnval2;
   char parm1;
   int  parm 2;
   long parm3;
} parmtab;
 
Then within each function simply pass a pointer to a variable of type parmtab.
 
void func1( parmtab *table1);
 
Each function could then read the relevant parameters from the structure, and write its return values to the required structure elements.  But the prototype for each function would remain consistent, irrespective of the data detail.
 
Regards,
Mac
 
0 Kudos
Reply