比较两个结构 C

comparing two structures C

我已经使用 typedef 定义了一个名为缓存的结构,并且我有一个将缓存作为参数的函数。我还创建了三个缓存实例。该函数在我的主要方法中调用,并根据情况将不同的缓存实例传递给它。如何在方法中检查哪个实例已通过?我尝试了 ==,但我得到的错误是 == illegal for struct。我该如何进行这项检查?

`

cache first_cache;
cache second_cache;
cache third_cache;

void function_example(cache user_cache, int size) {
    if (cache == first_cache) {
    ...
    }
    else if (cache == second_cache) {

    }

`

在这种情况下,您可以只比较地址:&first_cache == &second_cache。您希望将 cache 地址 作为参数传递,否则您将比较堆栈上的副本:

void function_example(cache * user_cache, int size) {
    if (user_cache == &first_cache) {

根据您最后两条评论,"for example cache_block *array_block; which I loop through using cache.array_block[i]... So if I want to do this here on the user_cache" 根据您发布的代码没有多大意义。唯一有意义的方法是,如果您的 struct cache 包含类型为 struct cache_block 的嵌套数组(例如,声明为 cache_block *array_block; 的成员已分配了已分配或静态声明填充的地址cache_block 类型的数组。如果不是这种情况,请澄清。特别是通过在您的问题中添加一个最小的、完整的和可验证的示例(参见:MCVE

您需要传递给 function_example 的内容将取决于您的结构是如何声明的以及在什么范围内。由于您只将一个结构传递给您的函数,因此您正在测试的 cache 必须是全局的,否则您将没有任何东西可以测试。在看不到结构是如何声明的或者确切的成员是什么的情况下,任何人都可以尝试通过一般意义上的结构比较来帮助您。

要比较您的结构,您有两种基本方法来进行比较。由于缺乏更好的词语,这两种方式可以描述为 deep 比较(您将每个结构成员的值或内容与相同结构的第二个结构中的值或内容进行比较类型,一次一个)或 shallow 比较(在其中对位于每个结构地址的内存作为一个整体进行按位比较)。 (shallow/deep 措辞通常与 copy 操作相关联,而不是比较,但它是直接相关的)。

由于在您的评论中您不想担心将结构作为指针处理,因此您将向您的函数传递结构的副本(或同时传递参考结构和要比较的结构)。无论您如何传递结构,两种比较途径都可用。要进行按位比较以确定您的结构是否相等,您可以使用 memcmp 来比较保存每个结构的两个内存块。在不知道您的 cache 是如何声明的情况下,要测试的引用结构作为 test 传递,而您的用户缓存结构作为 user 传递。比较的一个例子是:

void function_example(cache test, cache user)
{
    if (memcmp (&test, &user, sizeof test) == 0) {
        printf ("  test cache = user cache\n");
        return;
    }
    printf ("  test cache != user cache\n");
}

如果看不到代码的其余部分,就不可能匹配您的确切结构,但无论如何,上面的测试将提供两个结构的内容是否相同的比较,无论成员是什么。

(注意: 一个警告是,如果您的结构包含指向其他内存位置的指针,而这些内存位置不包含在您的结构占用的内存中。在这种情况下,您不能通过按位 单独比较结构来比较结构内容,并且需要逐个成员比较,其中还可以比较指针成员引用的内存)

使用上述函数和简单示例成员的完整示例可能如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int a;
    int b;
    char str[10];
} cache;

void function_example(cache test, cache user);

int main (void) {

    cache first_cache = { 1, 2, "cat" };
    cache second_cache = { 2, 1, "dog" };
    cache third_cache = { 1, 2, "cat" };

    cache test_cache;

    memcpy (&test_cache, &second_cache, sizeof second_cache);

    printf ("\ntesting first_cache:\n");
    function_example (test_cache, first_cache);

    printf ("\ntesting second_cache:\n");
    function_example (test_cache, second_cache);

    printf ("\ntesting third_cache:\n");
    function_example (test_cache, third_cache);

    return 0;
}

void function_example(cache test, cache user)
{
    if (memcmp (&test, &user, sizeof test) == 0) {
        printf ("  test cache = user cache\n");
        return;
    }
    printf ("  test cache != user cache\n");
}

输出

$ ./bin/cmpstruct

testing first_cache:
  test cache != user cache

testing second_cache:
  test cache = user cache

testing third_cache:
  test cache != user cache

显然这是一个简单的比较,不包含您在评论中寻求的循环比较,但没有更多,这只是一个猜测。如果您将编辑您的问题并添加其余代码,我很乐意与您进一步合作以帮助您实现您正在尝试的目标。