[英]copying a struct with a struct member to another struct
以下代码正确吗?
typedef struct
{
int x;
int y;
} OTHERSTRUCT;
struct DATATYPE
{
char a;
OTHERSTRUCT b;
}
// ...
// now we reserve two structs
struct DATATYPE structA;
struct DATATYPE structB;
// ... probably fill insome values
// now we copy structA to structB
structA = structB;
现在两个结构都完全相同吗? 甚至“ struct in the struct”?
谢谢!
是。
当一个结构变量分配给另一个,每个成员被复制一个接一个,包括其他结构OTHERSTRUCT
你有作为成员DATATYPE
。
是的,之后的内容相同。
struct DATATYPE
a
和b
之间可能存在填充字节,并且不能保证通过结构分配来复制这些填充字节。 这并不意味着它们就不会存在,只是您不能依赖于它们是否存在。
实际成员a
和b
将具有相同的值。 复制的效果会递归地应用于struct类型的成员,因此,是的,成员的成员也被复制了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.