如何比较C中的结构成员

huangapple go评论55阅读模式
英文:

how to compare structure members in C

问题

我有以下的代码片段。我试图创建一个比较数组和array2值的函数。函数com_read(); 负责加载array2的值,我不允许修改这个函数。所以在调用com_read()之后,我需要比较这两个数组,但由于我对C中的结构不太熟悉,所以不太确定如何做。我会感激任何建议。谢谢。

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>

#define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))

uint8_t array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

uint16_t sineLookupTable[] = {128, 218, 255, 218, 128, 37, 0, 37 };

#define test_failure 0;
#define test_success 1;

typedef struct
{
    int dr_assert;
    int dr_fail;
} dr_result_t;

typedef struct 
{
    uint8_t* Firstbuff;
    uint16_t Firstbuff_size;
    uint8_t *Secondbuff;
    uint16_t Secondbuff_size;
    dr_result_t result;

} dr_config_data;

struct i2c_open
{
    dr_config_data initconfig;
    dr_result_t expectedResult;
};

struct i2c_open readDATA[1] =
{
    {{array, ARRAY_LENGTH(array),array2, ARRAY_LENGTH(array2)}, 0x01}
};

int com_read(uint8_t id, dr_config_data* data) {
    int status = test_failure;

    //执行一些操作
    status = test_success;
}


int main() {
    // printf()函数用于显示引号内的字符串

    int result = 0;
    dr_result_t result2;

    result = com_read(0, &readDATA[0].initconfig);
    result2 = readDATA[0].expectedResult;
    printf("result2 = %d\n", result2);
    return 0;
}
英文:

I have the following code snippet. I'm trying to create a function that compares that values of array and array2. The function com_read(); loads values array2 and I'm not allowed to modify this function. So I need to compare the two arrays after calling com_read(); but I'm not sure how to do it since I'm not that familiar with structures in C. I would appreciate any suggestions. Thanks.

#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;inttypes.h&gt;

#define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))

uint8_t array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

uint16_t sineLookupTable[] = {128, 218, 255, 218, 128, 37, 0, 37 };

#define test_failure 0;
#define test_success 1;

typedef struct
{
	int dr_assert;
	int dr_fail;
} dr_result_t;

typedef struct 
{
	uint8_t* Firstbuff;
	uint16_t Firstbuff_size;
	uint8_t *Secondbuff;
	uint16_t Secondbuff_size;
	dr_result_t result;

} dr_config_data;

struct i2c_open
{
	dr_config_data initconfig;
	dr_result_t expectedResult;
};

struct i2c_open readDATA[1] =
{
	{{array, ARRAY_LENGTH(array),array2, ARRAY_LENGTH(array2)}, 0x01}
};

int com_read(uint8_t id, dr_config_data* data) {
	int status = test_failure;

    //do stuff
	status = test_success;
}


int main() {
	// printf() displays the string inside quotation

	int result = 0;
	dr_result_t result2;

	result = com_read(0, &amp;readDATA[0].initconfig);
	result2 = readDATA[0].expectedResult;
	printf(&quot;result2 = %d\n&quot;, result2);
    return 0;
}

答案1

得分: 2

Sure, here is the translated code portion:

> ... 创建一个函数来比较数组和array2的值。

由于这两个数组:

  • 元素中没有填充。
  • 是简单的整数。
  • 大小相同。
  • 类型相同。

... 使用 memcmp()

array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

bool equal = memcmp(array, array2, sizeof array) == 0;

如果上述条件不满足,代码可能需要采用不同的方法。

英文:

> ... create a function that compares that values of array and array2.

Since the 2 arrays:

  • Lack any padding in the elements.
  • Are simple integers.
  • The same size.
  • The same type.

... use memcmp().

array[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
uint8_t array2[5];

bool equal = memcmp(array, array2, sizeof array) == 0;

If the above conditions are not met, code may need a different approach.

答案2

得分: 1

我不太明白代码的结构和你的问题。但由于arrayarray2是全局变量,你可以通过访问它们的元素使用array[i]array2[i]来简单比较它们的内容,其中i是索引。

如果你想通过结构体来访问它们,你可以使用点号表示法,例如data->Firstbuff[i],其中data是指向结构体的指针。箭头->用于代替.,因为data是一个指针。你还可以使用(*data).Firstbuff[i]来首先解引用指针,然后访问结构体的字段。

我目前看到这段代码的另一个问题是array2的值从未设置过,这意味着它的所有元素都等于0。

英文:

I do not exactly understand the structure of the code and your question. But since array and array2 are global variables, you can compare their content simply by accesing their elements using array[i] and array2[i], where i is the index.

If you would like to access them through the struct, you can access struct elements using the dot notataion like data-&gt;Firstbuff[i], where data is the pointer to the struct. The arrow -&gt; is used instead of the ., because data is a pointer. You could also use (*data).Firstbuff[i] to first derefence the pointer and then access struct's field.

Another problem I currently see with this code is that values of array2 are never set, which means all its elements are equals to 0.

huangapple
  • 本文由 发表于 2023年6月9日 07:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定