typedef struct {
int speed;
bool electric;
} Car;
//== does not compile for structs: if (car1 == car2) is an error;
//the equality of values is a function that compares the fields
bool carEquals(const Car *a, const Car *b) {
return a->speed == b->speed && a->electric == b->electric;
}
Car car1 = {50, true};
Car car2 = {50, true};
Car *car3 = &car1;
//== on pointers is identity: is it the same object?
bool equal1 = &car1 == &car2;
//equal1 is false: two objects have two addresses
bool equal2 = &car1 == car3;
//equal2 is true: one object
bool equal3 = carEquals(&car1, &car2);
//equal3 is true: the same values in different objects
//memcmp(&car1, &car2, sizeof car1) is a trap: after the bool field
//the compiler adds padding bytes, they hold whatever was in memory,
//and memcmp reads them too; a double field has the same trouble
//with 0.0 and -0.0
printf("equal1 is %s\n", equal1 ? "true" : "false");
printf("equal2 is %s\n", equal2 ? "true" : "false");
printf("equal3 is %s\n", equal3 ? "true" : "false");