【问题标题】:Copy struct with reference in C [duplicate]在C中复制带有引用的结构[重复]
【发布时间】:2020-03-28 23:27:59
【问题描述】:

我想从另一个结构中复制一个结构,如下所示:

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


struct foo_struct{
    int foo;
    char *str;
};

static void foo(struct foo_struct *, struct foo_struct *);
void main(void) {
    int i;
    struct foo_struct *test = malloc(sizeof(struct foo_struct) * 5);
    struct foo_struct *cpy_struct = NULL;

    foo(test, cpy_struct);

    cpy_struct->foo = 20;
    //printf("%d\n", cpy_struct.foo);

    free(test);
}

static void foo(struct foo_struct *test, struct foo_struct *cpy){
    int i;
    for(i = 0; i < 5; i++)
        test[i].foo = i;    

    cpy = &test[2];
}

但是,当我修改此条目时:cpy_struct-&gt;foo = 20;,我遇到了分段错误。

当我没有使用struct foo_struct cpy_struct中的指针时,我修改了我的条目,但不是原始结构:

struct foo_struct cpy_struct = {0};

foo(test, &cpy_struct);

cpy_struct.foo = 20;
printf("%d\n", cpy_struct.foo); /* Display 20 */
printf("%d\n", test[2].foo);    /* Display 2 */

/* ... */

static void foo(struct foo_struct *test, struct foo_struct *cpy){
    int i;
    for(i = 0; i < 5; i++)
        test[i].foo = i;

    *cpy = test[2];
}

如何复制这个结构来更新特定结构中的值?

谢谢。

【问题讨论】:

  • 您需要将cpy_struct 的地址传递给foo() 并在该函数中更新cpy,例如*cpy = &amp;test[2]。在您的情况下,当foo() 返回时,函数中更新的cpy 中的值会丢失

标签: c struct structure


【解决方案1】:

谢谢,我从来没有使用过指针指针... 我正在尝试,它可以使用此代码:

static void foo(struct foo_struct *, struct foo_struct **);
void main(void) {
    int i;
    struct foo_struct *test = malloc(sizeof(struct foo_struct) * 5);
    struct foo_struct *cpy_struct = NULL;

    foo(test, &cpy_struct);

    cpy_struct->foo = 20;
    printf("%d\n", cpy_struct->foo); /* Display 20 */
    printf("%d\n", test[2].foo);     /* Display 20 */

    free(test);
}

static void foo(struct foo_struct *test, struct foo_struct **cpy){
    int i;
    for(i = 0; i < 5; i++)
        test[i].foo = i;    

    *cpy = &test[2];
}

【讨论】:

    【解决方案2】:

    第一个案例

    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct foo_struct{
        int foo;
        char *str;
    };
    
    static void foo(struct foo_struct *, struct foo_struct **);
    void main(void) {
        int i;
        struct foo_struct *test = malloc(sizeof(struct foo_struct) * 5);
        struct foo_struct *cpy_struct = NULL;
    
        foo(test, &cpy_struct);
    
        //cpy_struct->foo = 20;
        printf("%d\n", cpy_struct->foo);
    
        free(test);
    }
    
    static void foo(struct foo_struct *test, struct foo_struct **cpy){
        int i;
        for(i = 0; i < 5; i++)
            test[i].foo = i;    
    
        *cpy = &test[2];
    }
    

    您所做的是将 test[2] 的地址分配为 cpy_struct 的值。 你需要做的是将test[2]的地址分配给cpy_struct的地址。

    为此,您必须将 foo 的参数设置为指向指针的指针,以便当您将某些内容分配给 *cpy 时,它会分配给 cpy_struct 的地址,而不是它的值。

    我已经更正了代码并对其进行了测试。 printf 结果为 2,因为我注释掉了 cpy_struct->foo = 20;

    的赋值

    【讨论】:

      【解决方案3】:

      在第一个程序中,您必须通过引用传递指针。

      例如

      static void foo(struct foo_struct *test, struct foo_struct **cpy){
          int i;
          for(i = 0; i < 5; i++)
              test[i].foo = i;    
      
          *cpy = test + 2;
      }
      

      然后像这样调用函数

      foo(test, &cpy_struct);
      

      在这种情况下,main 中的指针 cpy_struct 将指向 test 指向的数组的第三个元素。

      在第二个程序中,您创建了一个结构类型的新对象并将test + 2 指向的对象复制到其中。因此,您有一个单独的对象。

      【讨论】:

        【解决方案4】:

        对于第二种情况,您需要固定分配顺序。当前代码将 test[2] 复制到 cpy_struct,然后将其修改为 20。输出将为 20。考虑以下情况

        struct foo_struct cpy_struct = {0};
        
        cpy_struct.foo = 20;
        
        foo(test, &cpy_struct);
        
        printf("%d\n", cpy_struct.foo); /* Display 2 */
        printf("%d\n", test[2].foo);    /* Display 2 */
        

        【讨论】:

        • 谢谢,但是,当我想要cpy_struct条目时,我想修改test[2].foo,而在第二种情况下,是不可能的。
        猜你喜欢
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-20
        • 2019-09-20
        相关资源
        最近更新 更多