LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Function pointer definition

Hi!
I'm trying to define an array of structures. These structures contain a field that is a pointer to a function. I want to initialize this array at its definition.
When I compile, I got compiler errors for some fields.
Here is my code:

////////// include file /////////////////
struct SCRIPTCOMMAND
{
char * command;
void (* functionpointer)(); // Pointer on the function (no parameters specified, depends on the function)
};

/* Function declarations */
void Message(char * title, char * message);

void SetResistor(unsigned short supplynr, double res);
void SetOutputState(unsigned short supplynr, enum STATE outstate);
void SetVoltage(unsigned short supplynr, double voltage);
void SetCurrent(unsigned short supplynr, double current);
void MeasureVolt(unsigned short supplynr, char * measurename, unsigned int interval_ms, unsigned long nbtimes);
void MeasureCurr(unsigned short supplynr, char * measurename, unsigned int interval_ms, unsigned long nbtimes);
void StartMeasureVolt(unsigned short supplynr, char * measurename, unsigned int interval_ms);
void StartMeasureCurr(unsigned short supplynr, char * measurename, unsigned int interval_ms);
void StopMeasureVolt(unsigned short supplynr);
void StopMeasureCurr(unsigned short supplynr);

void WriteReport(char * measurename, enum MEASUREPART measpart);

void CreateMeasure(char * measurename, enum MeasureType meastype);

/////////// C file ///////////////////////
struct SCRIPTCOMMAND ScriptCMDList[] =
{
"Message", Message,
"SetResistor", SetResistor,
"SetOutputState", SetOutputState,
"SetVoltage", SetVoltage,
"SetCurrent", SetCurrent,
"MeasureVolt", MeasureVolt,
"MeasureCurr", MeasureCurr,
"StartMeasureVolt", StartMeasureVolt,
"StartMeasureCurr", StartMeasureCurr,
"StopMeasureVolt", StopMeasureVolt,
"StopMeasureCurr", StopMeasureCurr,
"WriteReport", WriteReport,
"CreateMeasure", CreateMeasure
};

For the Message, WriteReport and CreateMeasure structures, the compilator doesn't report any error, but for the other structures, it says:

37, 40 Invalid initialization type; found 'pointer to void function(unsigned
short,double)' expected 'pointer to void function'.


Anyone has an idea about this error and why it doesn't report it for each structure?

Thank you very much!

Jean-Michel
0 Kudos
Message 1 of 2
(3,093 Views)
Hello

This is not the best way to do things. Whats probably happening is that with an empty parameter list,its expecting that the parameters are int size (4 bytes). So the char* works fine. ENUMS are ultimately ints. But when the parameters have doubles or shorts, its displaying an error.

Your best bet would probably be to give all the functions a standard prototype so that your function pointer is as type safe as possible and this way, you will not have to track which function takes what kind of parameter and how many.

Hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
0 Kudos
Message 2 of 2
(3,093 Views)