09-11-2008 09:44 AM
Hello,
I'am trying to work with the GetCtrlVal (, ,); command.
He is asking for a void pointer as value. i got an error like this: "null pointer argument to library function".
double *Temperature=0;
nt CVICALLBACK CallToggle (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
//SetCtrlVal (panel, PANEL_LED, 1);
GetCtrlVal (panelHandle, PANEL_Temperature,Temperature);
break;
}
return 0;
}
Thank you,
Jef
09-11-2008 09:50 AM
Well, "Temperature" in your code is actually a pointer which you have forced to zero and not inizialized afterwards.
You will have good results this way:
double Temperature = 0.0;
int CVICALLBACK CallToggle (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
//SetCtrlVal (panel, PANEL_LED, 1);
GetCtrlVal (panelHandle, PANEL_Temperature, &Temperature);
break;
}
return 0;
}
09-11-2008 09:52 AM
As a matter of fact, you are using a NULL pointer.
The declaration "double *Temperature = 0; " declares a pointer to a double value and gives the value 0 to the pointer (not to the double value)
You should malloc Temperature or, more easily:
*declare Temperature as a double
double Temperature = 0.0;
*Pass a pointer to Temperature to GetCtrlVal
GetCtrlVal (panelHandle, PANEL_Temperature,&Temperature);
Regards,
Nicolas
09-11-2008 09:58 AM
Thank you for the quick answer.
But now i really confused. you haven't initialize temperature as a pointer, and also you are using the ampersand before temperature in the command.
I thought this was a sign to pass a adress from the variable?
Could you please tell me what you exactly have done?
Thank you,
Jef
09-11-2008 10:06 AM - edited 09-11-2008 10:08 AM
The adress of any variable (whatever it's type) is a pointer.
&Temperature is a pointer to double.
You can always cast a 'pointer to whatever' towards a 'pointer to void' (whatever the value of the variable).
We could have written :
GetCtrlVal (panelHandle, PANEL_Temperature,(void *) &Temperature);
Best regards,
Nicolas
09-11-2008 10:22 AM - edited 09-11-2008 10:24 AM
Well, variables are normally passed to functions by value, that is the every modification of that variable in the called function has no influence on the value the variable has in the caller code. To grant that modifications in the called function reflects in the caller this must pass the address of the variable to the function so that its modifications reflect in variable value in the caller. This is normally done by using pointers (as Nicolas told you, & operator returns the pointer to the variable).
What you have done is creating a pointer to a double but not assigning it to anything, thus the unitialized pointer error in GetCtrlVal: this can be eliminated by assigning the pointer to an actual variable. So your alternatives are those:
1. Passing the address of a double variable
double temperature = 0.0;
GetCtrlVal (panel, control, &temperature);
2. Passing a pointer that points to a double variable
double x = 0.0;
double *temperature = NULL;
temperature = &x;
GetCtrlVal (panel, control, temperature);
with the exact same result.
As a last note, GetCtrlVal expects that the pointer points to a variable with a datatype coherent to the control addressed: you cannot pass a pointer to a int to read a numeric with double precision type.
09-15-2008 01:31 AM
Ok thank you for explanation.
Jef