From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use void pointer in function

Solved!
Go to solution

I am trying to convert float/int/str information to string and store in an array.  Can I use a single function to do that?  I am getting errors under Labwindows 

82, 34 error: invalid operands to binary expression ('float *' and 'float')

I think I didn't use void pointer correctly, but why?  Thanks ahead.

 

main{

TestReport(Test_Description, 0.2,&MeasureValue,0.4, TESTREPORT_FORMAT_FLOAT,"Volt");

}

 

int TestReport(char description[], float Lowlimit, void *Result, float Highlimit, char datatype,
char unit[])
{

char TempTest[256];

 

strcat(Test_Report_Text, description); //report item header
switch (datatype){
case TESTREPORT_FORMAT_INT:
if (((int *)Result > (int)Lowlimit)&&((int *)Result < (int)Highlimit)) strcat(TempTest, "Pass");
strcat(TempTest, "*Fail");
break;
case TESTREPORT_FORMAT_FLOAT:
if (((float *)Result > Lowlimit)&&((float *)Result < Highlimit)) strcat(TempTest, "Pass");
strcat(TempTest, "*Fail");
break;
case TESTREPORT_FORMAT_STR:
break;
}
strcat(Test_Report_Text, TempTest);
return 0;
}

 

0 Kudos
Message 1 of 7
(3,209 Views)
Solution
Accepted by topic author SilverMan

The error is quite clear: you cannot compare a pointer (float *) to a float value. You should modify your expression this way:

if ((*(float *)Result > Lowlimit) && (*(float *)Result < Highlimit)) strcat (TempTest, "Pass");

Beware that the same applies to the int case: you don't get a warning since a pointer effectively is an integer value, but you are actually comparing the pointer (=address) value with the limits instead of the actual value of the variable addressed from the pointer.



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?
Message 2 of 7
(3,186 Views)

Hi RobertoBozzolo,

 

Thank you so much for answering the questions, it is working now.  But I have an additional question:

In the function which contains the void * variable, I found in C language I have to use a extra variable in order to get the correct value(other than directly using the pointer variable). For example:

 

float Temp = *(float *)Result;

if(Temp > Lowlimit) do something;   //this will work

 

if(*(float *)Result > Lowlimit) do something; //this will not work

 

Can anyone explain why I have to the variable indirectly?

 

Thank you! 

 

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

I'm not aware of such a constraint: the code as I wrote should work correctly. Did you find some problems?



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 4 of 7
(3,155 Views)

if I directly use the code:

if ((*(float *)Result > Lowlimit) && (*(float *)Result < Highlimit)) strcat (TempTest, "Pass");

 it wouldn't work. 

Unless I store the float value into another variable first and then it will work as it it suppose to be.

float tempresult = *(float *)Result;

if (tempresult > Lowlimit) && (temp < Highlimit) strcat (TempTest, "Pass");

 

I am just curious why the first line of code doesn't work, in my understanding, both types of C code have the same interpretation.

0 Kudos
Message 5 of 7
(3,119 Views)

What kind of error/behavior are you getting?

0 Kudos
Message 6 of 7
(3,109 Views)

How about when you try:

 

if (((*(float *)Result) > Lowlimit) && ((*(float *)Result) < Highlimit)) strcat (TempTest, "Pass");

It should be equivalent according to the C operator precedence standard, but this would make sure it is not some operator precedence messup. 

Rolf Kalbermatter
My Blog
0 Kudos
Message 7 of 7
(3,057 Views)