ni.com is experiencing intermittent service disruptions.

Support teams are actively working on the resolution.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

function pointers with input arguments

How would I setup function pointers for a program in which different functions have different input types and amounts?

 

For example I have the below functions:

 

 

int _VI_FUNC Supply_ConfigOutputEnabled(int devs,int instrumentHandle, char *channelName,int enabled);
int _VI_FUNC Supply_ConfigureOutputRange(int devs,int instrumentHandle, char *channelName,int ZeroVoltageOneCurrent,int LowHigh);
int _VI_FUNC Supply_ConfigureCurrentLimit(int devs,int instrumentHandle, char *channelName,double currentLimit);
int _VI_FUNC Supply_ConfigureOVP(int devs,int instrumentHandle, char *channelName,int disableEnable, double OVP);
int _VI_FUNC Supply_SetVoltage(int devs,int instrumentHandle, char *channelName,double voltage);

 

int _VI_FUNC Supply_ConfigOutputEnabled(int devs,int instrumentHandle, char *channelName,int enabled);

int _VI_FUNC Supply_ConfigureOutputRange(int devs,int instrumentHandle, char *channelName,int ZeroVoltageOneCurrent,int LowHigh);

int _VI_FUNC Supply_ConfigureCurrentLimit(int devs,int instrumentHandle, char *channelName,double currentLimit);

int _VI_FUNC Supply_ConfigureOVP(int devs,int instrumentHandle, char *channelName,int disableEnable, double OVP);

int _VI_FUNC Supply_SetVoltage(int devs,int instrumentHandle, char *channelName,double voltage);

 

I want to populate a Ring for the user to select which function to call, and be able to input the argument(s).  The arguments can be in the form of a string and can convert behind the scenes.

 

Thus far, i have something like this:

 

typedef int (*function)(void);
typedef struct
{
	char *function_name;
	function func;
	
} function_array;

function_array Supply_Config_Funcs[] = {
"ConfigOutputEnabled", 		Supply_ConfigOutputEnabled(int devs,int instrumentHandle,char *channelName,int enabled);
"ConfigureOutputRange", 	Supply_ConfigureOutputRange(int devs,int instrumentHandle, char *channelName,int ZeroVoltageOneCurrent,int LowHigh);
"ConfigureCurrentLimit", 	Supply_ConfigureCurrentLimit(int devs,int instrumentHandle, char *channelName,double currentLimit);
"ConfigureOVP",			Supply_ConfigureOVP(int devs,int instrumentHandle, char *channelName,int disableEnable, double OVP);
"SetVoltage",			Supply_SetVoltage(int devs,int instrumentHandle, char *channelName,double voltage);	
	
}

 

But obviously this generates a lot of errors.  Do I need to have a different function pointer for every combination of input arguments?  Would i need to set all unused ones to NULL then?

 

Currently my only other solution is to just assign each "String" a value and ensure that value lines up with the function call in a GIANT case statement.

 

This is my first time doing funtion pointers!? So thanks for the feedback.

 

Nick

 

0 Kudos
Message 1 of 3
(2,980 Views)

Hello Nick,

 

I believe you might need to assign a function pointer for each set of parameters. You will then set the function pointer equal to the pointer associated with the function. Here is a link that gives a tutorial on how to use function pointers which I believe would be useful to go over. Let me know if this helps out. Have a great day!

 

Best Regards,

 

Adam G  

National Instruments
Applications Engineer
Message 2 of 3
(2,944 Views)

Nick:

 

Generally function pointers are used best when all of the functions involved have the same prototype (the same number and type of input parameters, the same type return value).  If these prototypes are different, you end up needing a switch statement of if..else structure anyway to pass the correct parameters.  In that case, I think it would be less confusing just to use the switch to call the functions directly.

 

The next higher level of the good link that Nick sent is here: http://www.newty.de/fpt/index.html.

In the Introduction, there is a clear and simple example comparing using function pointers to using a switch statement and calling functions directly.  You can see that all the prototypes of the different functions are the same, so the call using function pointers looks the same, regardless of which function you're pointing to.  That's the key to using function pointers easily and efficiently.

Message 3 of 3
(2,913 Views)