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.
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.
09-17-2017 04:45 PM
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;
}
Solved! Go to Solution.
09-18-2017 01:43 AM
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.
09-19-2017 07:34 AM - edited 09-19-2017 07:35 AM
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!
09-19-2017 11:03 AM
I'm not aware of such a constraint: the code as I wrote should work correctly. Did you find some problems?
09-21-2017 02:39 PM - edited 09-21-2017 02:39 PM
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.
09-22-2017 03:31 PM
What kind of error/behavior are you getting?
10-02-2017 09:03 AM - edited 10-02-2017 09:05 AM
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.