在C中复制结构数组 [英] Copying arrays of structs in C

查看:91
本文介绍了在C中复制结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用C语言已有很长时间了,这使我发疯.我有一个结构数组,我需要创建一个将一个数组复制到另一个数组的函数(我需要一个精确的副本),但是我不知道如何定义函数调用.我想我需要使用指针,但是当我尝试使用它时,会出现错误.

It's been a long since I don't use C language, and this is driving me crazy. 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);

最后一条声明给我发了一个错误.就像我说的那样,用C语言编程已经很久了,所以我现在有点迷路了:(

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 :(

推荐答案

应该这样做:

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

编辑:顺便说一句:它将在b中保存a的副本.

EDIT: By the way: It will save a copy of a in b.

这篇关于在C中复制结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆