I'm comparing using memcmp()
two variables of the same struct (the struct has union in it).
The variables are in two arrays and I'm running a loop where each iteration I do memcmp(&arr1[i], &arr2[i], sizeof(arrtype))
.
When debugging I see that memcmp returns -1, but looking at the two variables and their values, I see that the variables has equal values. These arrays are zeroed with memset at the beginning.
- So does anybody know why
memcmp
returns -1 and not 0? - Is there a better way to do what I need (compare two memory blocks)?
code:
typedef struct type1 {
int version;
union {
option1_t opt1;
option2_t opt2;
} union_t;
} type1_t;
typedef struct type0 {
type1_t member1;
type2_t member2;
type3_t member3;
type4_t member4;
type5_t member;
} type0_t;
type0_t arr1[SIZE];
type0_t arr2[SIZE];
memset(arr1, 0, SIZE * sizeof(type0_t));
memset(arr2, 0, SIZE * sizeof(type0_t));
/* doing irrelevant stuff... */
/* get values into arr1, arr2 ... */
/* comparing both arrays in for loop*/
value = memcmp(&arr1[i], &arr2[i], sizeof(type0_t));