C语言 将结构复制到字符数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26430446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:28:35  来源:igfitidea点击:

Copy struct into char array

cstruct

提问by yiwei

I am learning C and have a question about structs.

我正在学习 C 并且有一个关于结构的问题。

I have a

我有一个

struct myStruct {
    char member1[16];
    char member2[10];
    char member3[4];
};

This should take at least 30 bytes of memory to store. Would it be possible to copy all of this data into the variable char foo[30]? What would be the syntax?

这应该至少需要 30 字节的内存来存储。是否可以将所有这些数据复制到变量中char foo[30]?语法是什么?

回答by Carl Norum

You can't just directly copy the whole thing, because the compiler may arbitrarily decide how to pad/pack this structure. You'll need three memcpycalls:

你不能直接复制整个东西,因为编译器可能会任意决定如何填充/打包这个结构。你需要三个memcpy电话:

struct myStruct s;
// initialize s
memcpy(foo,                                       s.member1, sizeof s.member1);
memcpy(foo + sizeof s.member1,                    s.member2, sizeof s.member2);
memcpy(foo + sizeof s.member1 + sizeof s.member2, s.member3, sizeof s.member3);

回答by Kerrek SB

The size of struct myStructis sizeof(struct myStruct)and nothing else. It'll be at least 30, but it could be any larger value.

struct myStructis的大小,仅此sizeof(struct myStruct)而已。它至少是 30,但它可以是更大的值。

You can do this:

你可以这样做:

char foo[sizeof(struct myStruct)];

struct myStruct x; /* populate */

memcpy(foo, &x, sizeof x);

回答by Vlad from Moscow

According to the C Standard (6.2.6 Representations of types)

根据 C 标准(6.2.6 类型表示)

4 Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value.

4 存储在任何其他对象类型的非位域对象中的值由 n × CHAR_BIT 位组成,其中 n 是该类型对象的大小,以字节为单位。该值可以复制到 unsigned char [n] 类型的对象中(例如,通过 memcpy);结果字节集称为值的对象表示。

So you can write simply

所以你可以简单地写

unsigned char foo[sizeof( struct myStruct )];
struct myStruct s = { /*...*/ };

memcpy( foo, &s, sizeof( struct myStruct ) );

Take into account that you could copy the data members separatly in one array. For example

考虑到您可以将数据成员单独复制到一个数组中。例如

unsigned char foo[30];
struct myStruct s = { /*...*/ };

unsigned char *p = foo;
memcpy( p, s.member1, sizeof( s.member1 ) );
memcpy( p += sizeof( s.member1 ), s.member2, sizeof( s.member2 ) );
memcpy( p += sizeof( s.member2 ), s.member3, sizeof( s.member3 ) );

回答by Sparky

Yes, it is possible.

对的,这是可能的。

There are different ways you can go about doing this. Below are the two simplest methods.

你可以通过不同的方式来做到这一点。下面介绍两种最简单的方法。

struct myStruct  myVar;

/* Initialize myVar */
...

memcpy (foo, &myVar, sizeof (myStruct));

Or if you are dealing with a pointer ...

或者,如果您正在处理指针...

struct myStruct *  myVarPtr;

/* Initialize myVarPtr */
...

memcpy (foo, myVarPtr, sizeof (myStruct));

Note that when copying a structure to/from a character array like this, you have to be very careful as structure sizes are not always what you might first think. In your particular case, there might not be any issues; but in general, you should at least be aware of potential padding, alignment and type size issues that may change the size of your structure.

请注意,将结构复制到/从这样的字符数组复制时,您必须非常小心,因为结构大小并不总是您首先想到的。在您的特定情况下,可能没有任何问题;但总的来说,您至少应该意识到可能会改变结构大小的潜在填充、对齐和类型大小问题。

Hope this helps.

希望这可以帮助。

回答by Awais

Pretty simple with memcpy.

使用 memcpy 非常简单。

char foo[30];
struct myStruct s;

s.member1 = //some data
s.member2 = //some data
s.member3 = //some data

memcpy(foo, &s, 30);

回答by Triton Man

you could do the following if you have a myStruct variable named st:

如果您有一个名为 st 的 myStruct 变量,您可以执行以下操作:

strcpy(foo, st.member1);
strcat(foo, st.member2);
strcat(foo, st.member3);