在 C 中复制带有引用的结构
Copy struct with reference in C
我想从另一个结构复制一个结构,如下所示:
#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->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];
}
如何复制此结构以更新特定结构中的值?
谢谢。
对于第二种情况,您需要固定赋值顺序。当前代码将 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 */
在第一个程序中,您必须通过引用传递指针。
例如
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
指向的对象复制到其中。因此,您有一个单独的对象。
第一个案例
#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;
的赋值
谢谢,我从来没有用过指针对指针...
我正在尝试,它适用于此代码:
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];
}
我想从另一个结构复制一个结构,如下所示:
#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->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];
}
如何复制此结构以更新特定结构中的值?
谢谢。
对于第二种情况,您需要固定赋值顺序。当前代码将 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 */
在第一个程序中,您必须通过引用传递指针。
例如
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
指向的对象复制到其中。因此,您有一个单独的对象。
第一个案例
#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;
的赋值谢谢,我从来没有用过指针对指针... 我正在尝试,它适用于此代码:
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];
}