In the below code, when I compare a struct to 0, it gives me the error "error: invalid operands to binary != (have ‘struct <anonymous>’ and ‘int’)". The problem is I will have an array of structs and some of the indexes might not contain a struct, so how can I check whether a specific index of array contains a struct or not?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
} sensor;
static sensor sensors[3] = {0};
static char msgs[3][100];
static char wholeMsg[300];
sensor createSensor(unsigned long long int address, float current, unsigned char pressure_units)
{
sensor temp;
temp.address = address;
temp.current = current;
temp.pressure_units = pressure_units;
return temp;
}
// It's not a problem if one of sensors does not report,
// because we can reference sensors by their address
// but make sure array indexes are correct
void checkSerialHart(int sensor_id)
{
sensors[sensor_id-1] = createSensor(10000*sensor_id,sensor_id+3.0,sensor_id);
}
void sendToIridium(void)
{
int i,j;
char msg[100];
// have to use 3 here and not sensorCount because sensors won't be in order
for(i=0,j=0;i<3;i++)
{
if(sensors[i] != 0){
sprintf(msg,"address:%llu,current:%f,pressure_units:%u",
sensors[i].address, sensors[i].current, sensors[i].pressure_units);
strcpy(msgs[j],msg);
++j;
}
}
for(i=0;i<sizeof(msgs)/sizeof(msgs[0]); i++)
{
if(i=0)
{
sprintf(wholeMsg,"%s\n",msgs[i]);
}
else
{
sprintf(wholeMsg + strlen(wholeMsg),"%s\n",msgs[i]);
}
}
printf("iridium msg: %s",wholeMsg);
}
int main(int argc, char *argv[])
{
checkSerialHart(1);
checkSerialHart(3);
if(sizeof(sensors) > 0)
{
sendToIridium();
}
else {
printf("No sensors replied\n");
}
}