[英]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.