LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Void Pointers

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

0 Kudos
Message 1 of 7
(3,785 Views)

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;
}



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 7
(3,781 Views)

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

0 Kudos
Message 3 of 7
(3,777 Views)

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

 

0 Kudos
Message 4 of 7
(3,774 Views)

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

 

Message Edité par NicoLeach le 09-11-2008 05:07 PM
Message Edité par NicoLeach le 09-11-2008 05:08 PM
0 Kudos
Message 5 of 7
(3,767 Views)

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.

Message Edited by Roberto Bozzolo on 09-11-2008 05:24 PM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 7
(3,762 Views)

Ok thank you for explanation.

 

Jef

0 Kudos
Message 7 of 7
(3,709 Views)