How can you check two structs in C in a if statement if they are the same without going threw all the items of the struct seperately?
the == statement doesn't work
thanx in advance
Printable View
How can you check two structs in C in a if statement if they are the same without going threw all the items of the struct seperately?
the == statement doesn't work
thanx in advance
You could do a memcmp:Note that we now have an intriguing problem. On my system, it says they're identical. This is because it's noticed the two "Hello!" strings are the same, and folded them into the same pointer. This means that the memory comparison determines they're equal.Code:#include <stdio.h>
typedef struct {
int x;
char *str;
double num;
} thing_t;
int main(void) {
thing_t first = { 5, "Hello!", 1.2 };
thing_t second = { 5, "Hello!", 1.2 };
if(!memcmp(&first, &second, sizeof(thing_t))) {
printf("Structures are identical\n");
} else {
printf("Structures are different\n");
}
return 0;
}
Those two strings could be the same, but have *different* pointer values. In this case, the structures would be different, but what they *meant* would be the same.
How you want them to be compared is up to you. If they're simple (i.e., just numbers, no pointers) then you can get away with memcmp(). I would suggest having a specific compare function for anything else:Note that I've also added a specific check for the floating-point numbers to try and avoid rounding errors. I'm not convinced about that, so I'll let someone else make any comments if they wants to.Code:#include <stdio.h>
#include <assert.h>
#include <float.h>
#include <math.h>
struct thing_t {
int x;
char *str;
double num;
};
int compare_things(const struct thing_t* first, const struct thing_t* second) {
assert(first != NULL && second != NULL);
return (first->x == second->x && !strcmp(first->str, second->str) && fabs(first->num - second->num) <= DBL_EPSILON);
}
int main(void) {
struct thing_t first = { 5, "", 1.2 };
struct thing_t second = { 5, "", 1.2 };
char a[] = "Hello";
char b[] = "Hello";
first.str = a;
second.str = b;
if(a == b) {
printf("Strings are the same array...shouldn't be!\n");
} else {
printf("Good, strings are different\n");
}
printf("\n");
if(compare_things(&first, &second)) {
printf("Structures are identical\n");
} else {
printf("Structures are different\n");
}
return 0;
}
I always thought that == would automatically compare memberwise...
In C++ you can of course overload the == operator, in C you'll have to go with parksie's approach.
Code:[mike@relativity junk]$ gcc -o structs structs.c
structs.c: In function `main':
structs.c:42: invalid operands to binary ==
Somehow I'm not getting memcmp above:
memcmp returns 0 when equal.
memcmp will compare two separate memory locations and work
correctly. In most C implementations it is what is behind strcmp().
It doesn't have anything to do with memory locations or different datatypes. If the bytes at the two addresses match you get a zero return.
Which part confuses you then?
What I was trying to demonstrate is that even if memcmp decides they're different, they may actually mean the same (i.e. have the same string data, but a different pointer).