Search code examples
ccompound-literals

#define a constant struct


Let's say I have a struct:

struct location
{
     int x;
     int y;
};

Then I want to define a invalid location for use later in the program:

#define INVALID_LOCATION (struct location){INT_MAX,INT_MAX}

However when I use that in my program, it ends up in an error:

struct location my_loc = { 2, 3 };
if (my_loc == INVALID_LOCATION)
{
     return false;
}

This won't compile. Is it not legal to use compound literals that way? I get the error:

Invalid operands to binary expression ('struct location' and 'struct location')


Solution

  • You can't compare structures for equality with ==.