Thread: error: invalid operands to binary != (have ‘struct <anonymous>’ and ‘int’)

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    error: invalid operands to binary != (have ‘struct <anonymous>’ and ‘int’)

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

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    sensors is an array of type sensor. Thus, sensors[i] is a single instance of a sensor. You typedef'ed sensor as a struct (anonymous since you did struct { instead of struct some_name {). You are trying to compare a whole struct to an integer value 0. A struct can not be compared to an int.
    Last edited by anduril462; 04-07-2014 at 05:37 PM.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    62
    NULL doesn't work either, so it appears you cannot set 0 and NULL as values of array of structs, because if you want to check if the current index of array is NULL, there is no way to check since it will produce the above error.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You don't have an array of pointers to struct, you have an array of structs, thus sensors[i] is a struct, not a pointer to struct. Since you don't have a pointer, NULL doesn't make sense. Since you don't have a number, 0 doesn't make sense. A struct is a collection of things, each with their own value. You can't compare a whole bunch of things to a single value. You need to figure out what it is you are really trying to check for. Describe that in English and we can help you get the right code to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'error: invalid operands to binary *' problem
    By Kevin Breslin in forum C Programming
    Replies: 3
    Last Post: 04-05-2013, 04:02 PM
  2. Getting error: invalid operands to binary &
    By P6nn in forum C Programming
    Replies: 3
    Last Post: 05-06-2012, 04:27 AM
  3. Invalid Operands to Binary Error
    By MikeyVeeDubb in forum C Programming
    Replies: 17
    Last Post: 09-23-2009, 04:13 AM
  4. error: invalid operands to binary %
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-18-2008, 09:21 AM
  5. error: invalid operands to binary *
    By cblix in forum C Programming
    Replies: 5
    Last Post: 01-29-2006, 08:45 PM