小编the*_*ard的帖子

为什么memcmp返回-1虽然相等

我正在使用memcmp()相同结构的两个变量进行比较(结构中有结合).变量在两个数组中,我正在运行一个循环,我在每次迭代中都这样做memcmp(&arr1[i], &arr2[i], sizeof(arrtype)).

调试时我发现memcmp返回-1,但是看两个变量及其值,我看到变量的值相等.这些数组在开始时使用memset归零.

  1. 所以有人知道为什么memcmp返回-1而不是0?
  2. 有没有更好的方法来做我需要的(比较两个内存块)?

码:

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 …
Run Code Online (Sandbox Code Playgroud)

c arrays struct block memcmp

1
推荐指数
1
解决办法
2136
查看次数

标签 统计

arrays ×1

block ×1

c ×1

memcmp ×1

struct ×1