C语言 在 C 中将一个结构分配给另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2302351/
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
Assign one struct to another in C
提问by shreyasva
Can you assign one instance of a struct to another, like so:
您可以将结构的一个实例分配给另一个实例,如下所示:
struct Test t1;
struct Test t2;
t2 = t1;
I have seen it work for simple structures, bu does it work for complex structures?
How does the compiler know how to copy data items depending on their type, i.e. differentiating between an intand string?
我已经看到它适用于简单的结构,但它适用于复杂的结构吗?
编译器如何知道如何根据类型复制数据项,即区分 anint和 string?
回答by fabrizioM
Yes if the structure is of the same type. Think it as a memory copy.
是的,如果结构是相同类型的。将其视为内存副本。
回答by shreyasva
Yes, assignment is supported for structs. However, there are problems:
是的,结构支持赋值。但是,存在以下问题:
struct S {
char * p;
};
struct S s1, s2;
s1.p = malloc(100);
s2 = s1;
Now the pointers of both structs point to the same block of memory - the compiler does not copy the pointed to data. It is now difficult to know which struct instance owns the data. This is why C++ invented the concept of user-definable assignment operators - you can write specific code to handle this case.
现在两个结构的指针都指向同一个内存块——编译器不会复制指向的数据。现在很难知道哪个结构实例拥有数据。这就是 C++ 发明用户可定义赋值运算符概念的原因——您可以编写特定代码来处理这种情况。
回答by Arun Kaushal
First Look at this example :
首先看这个例子:
The C code for a simple C program is given below
下面给出了一个简单 C 程序的 C 代码
struct Foo {
char a;
int b;
double c;
} foo1,foo2;
void foo_assign(void)
{
foo1 = foo2;
}
int main(/*char *argv[],int argc*/)
{
foo_assign();
return 0;
}
The Equivalent ASM Code for foo_assign() is
foo_assign() 的等效 ASM 代码是
00401050 <_foo_assign>:
401050: 55 push %ebp
401051: 89 e5 mov %esp,%ebp
401053: a1 20 20 40 00 mov 0x402020,%eax
401058: a3 30 20 40 00 mov %eax,0x402030
40105d: a1 24 20 40 00 mov 0x402024,%eax
401062: a3 34 20 40 00 mov %eax,0x402034
401067: a1 28 20 40 00 mov 0x402028,%eax
40106c: a3 38 20 40 00 mov %eax,0x402038
401071: a1 2c 20 40 00 mov 0x40202c,%eax
401076: a3 3c 20 40 00 mov %eax,0x40203c
40107b: 5d pop %ebp
40107c: c3 ret
As you can see that a assignment is simply replaced by a "mov" instruction in assembly, the assignment operator simply means moving data from one memory location to another memory location. The assignment will only do it for immediate members of a structures and will fail to copy when you have Complex datatypes in a structure. Here COMPLEX means that you cant have array of pointers ,pointing to lists.
正如您所看到的,赋值在汇编中被简单地替换为“mov”指令,赋值运算符仅表示将数据从一个内存位置移动到另一个内存位置。分配只会对结构的直接成员执行,并且当结构中有复杂数据类型时将无法复制。这里的 COMPLEX 意味着您不能拥有指向列表的指针数组。
An array of characters within a structure will itself not work on most compilers, this is because assignment will simply try to copy without even looking at the datatype to be of complex type.
结构中的字符数组本身在大多数编译器上不起作用,这是因为赋值只会尝试复制,甚至不会查看数据类型是否为复杂类型。
回答by Thomas Pornin
This is a simple copy, just like you would do with memcpy()(indeed, some compilers actually produce a call to memcpy()for that code). There is no "string" in C, only pointers to a bunch a chars. If your source structure contains such a pointer, then the pointer gets copied, not the chars themselves.
这是一个简单的副本,就像您所做的一样memcpy()(实际上,某些编译器实际上会生成memcpy()对该代码的调用)。C 中没有“字符串”,只有指向一堆字符的指针。如果您的源结构包含这样一个指针,那么指针将被复制,而不是字符本身。
回答by Clifford
Did you mean "Complex" as in complex number with real and imaginary parts? This seems unlikely, so if not you'd have to give an example since "complex" means nothing specific in terms of the C language.
您的意思是“复数”是指具有实部和虚部的复数吗?这似乎不太可能,所以如果不是,您必须举一个例子,因为“复杂”在 C 语言方面没有任何具体意义。
You will get a direct memory copy of the structure; whether that is what you want depends on the structure. For example if the structure contains a pointer, both copies will point to the same data. This may or may not be what you want; that is down to your program design.
您将获得结构的直接内存副本;这是否是你想要的取决于结构。例如,如果结构包含一个指针,则两个副本将指向相同的数据。这可能是也可能不是您想要的;这取决于您的程序设计。
To perform a 'smart' copy (or a 'deep' copy), you will need to implement a function to perform the copy. This can be very difficult to achieve if the structure itself contains pointers and structures that also contain pointers, and perhaps pointers to such structures (perhaps that's what you mean by "complex"), and it is hard to maintain. The simple solution is to use C++ and implement copy constructors and assignment operators for each structure or class, then each one becomes responsible for its own copy semantics, you can use assignment syntax, and it is more easily maintained.
要执行“智能”复制(或“深度”复制),您需要实现一个函数来执行复制。如果结构本身包含指针和包含指针的结构,并且可能包含指向此类结构的指针(也许这就是您所说的“复杂”),这可能很难实现,并且很难维护。简单的解决方案是使用 C++ 并为每个结构或类实现复制构造函数和赋值运算符,然后每个结构或类负责自己的复制语义,您可以使用赋值语法,并且更容易维护。

