简体   繁体   English

在C中复制结构数组

[英]Copying arrays of structs in C

It's been a long since I don't use C language, and this is driving me crazy. 我已经很久没用C语言了,这让我发疯了。 I have an array of structs, and I need to create a function which will copy one array to another (I need an exact copy), but I don't know how to define the function call. 我有一个结构数组,我需要创建一个将一个数组复制到另一个数组的函数(我需要一个精确的副本),但我不知道如何定义函数调用。 I guess I need to use pointers, but when I try it gives me an error. 我想我需要使用指针,但是当我尝试它时会给我一个错误。

struct group{
    int weight;
    int x_pos;
    int y_pos;
    int width;
    int height;
};

struct group a[4];
struct group b[4];

copySolution(&a, &b);

That last declaration send me an error. 最后一个声明给我发错了。 As I said, it's been a long since programming in C, so I'm a bit lost right now :( 就像我说的那样,用C编程已经很久了,所以我现在有点迷失了:(

That should do it: 应该这样做:

memcpy(&b, &a, sizeof(a));

EDIT : By the way: It will save a copy of a in b . 编辑 :顺便说一句:这将节省的拷贝ab

As Johannes Weiß says, memcpy() is a good solution. 正如JohannesWeiß所说, memcpy()是一个很好的解决方案。

I just want to point out that you can copy structs like normal types: 我只想指出你可以复制像普通类型的结构:

for (i=0; i<4; i++) {
    b[i] = a[i]; /* copy the whole struct a[i] to b[i] */
}

This has a feel of poorly masqueraded homework assignment... Anyway, given the predetermined call format for copySolution in the original post, the proper definition of copySolution would look as follows 这有伪装不良家庭作业的感觉......总之,给出了预定的通话格式copySolution在原岗位,的适当定义copySolution将如下所示

void copySolution(struct group (*a)[4], struct group (*b)[4])
{
  /* whatever */
}

Now, inside the copySolution you can copy the arrays in any way you prefer. 现在,在copySolution您可以以您喜欢的任何方式复制数组。 Either use a cycle 要么使用一个循环

void copySolution(struct group (*a)[4], struct group (*b)[4])
{
  const struct group *pb, *pbe;
  struct group *pa;

  for (pa = *a, pb = *b, pbe = pb + sizeof *b / sizeof **b; 
       pb != pbe; 
       ++pa, ++pb)
    *pa = *pb;
}

or use memcpy as suggested above 或使用上面建议的memcpy

void copySolution(struct group (*a)[4], struct group (*b)[4])
{
  memcpy(b, a, sizeof *b);
}

Of course, you have to decide first which direction you want your arrays to be copied in. You provided no information, so everyone just jumped to some conclusion. 当然,你必须首先决定你希望你的阵列被复制到哪个方向。你没有提供任何信息,所以每个人都只是得出了一些结论。

In my case previous solutions did not work properly! 在我的情况下,以前的解决方案无法正常工作 For example, @Johannes Weiß's solution did not copy "enough" data (it copied about half of the first element). 例如,@JohannesWeiß的解决方案没有复制“足够”的数据(它复制了大约一半的第一个元素)。
So in case, somebody needs a solution, that will give you correct results, here it is: 所以万一有人需要一个解决方案,这会给你正确的结果,这里是:

int i, n = 50;
struct YourStruct *a, *b;

a = calloc(n, sizeof(*a));
b = malloc(n * sizeof(*b));
for (i = 0; i < n; ++i) { 
    // filling a
}

memcpy(b, a, n * sizeof(*a)); // <----- see memcpy here

if (a != NULL) free(a);
a = calloc(n*2, sizeof(*a));

memcpy(a, b, n * sizeof(*b)); // <------ see memcpy here again

Some notes, I used calloc for a, because in the '// filling a' part I was doing operations that required initialized data. 一些注意事项,我使用calloc作为a,因为在'// fill a'部分我正在进行需要初始化数据的操作。

The compiler has no information about the size of the array after passing them as pointer into a function. 将它们作为指针传递给函数后,编译器没有关于数组大小的信息。 So you often need a third parameter: The size of the arrays to copy. 因此,您经常需要第三个参数:要复制的数组的大小。

A solution (without any error checking) could be: 解决方案(没有任何错误检查)可能是:

void copySolution(struct group* a, struct group* b, size_t size) {
    memcpy(a, b, size * sizeof(*a));
} 

The easiest way is probably 最简单的方法可能就是

 b=a

although a solution with memcpy() will also work. 虽然使用memcpy()的解决方案也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM