C语言 将一个指针内容复制到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39938648/
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
Copy one pointer content to another
提问by malajedala
I thought I've read somewhere that when using pointers and we want to copy the content of one to another that there are two options:
我想我在某处读到过,当使用指针时,我们想将一个的内容复制到另一个,有两种选择:
- using memcpy or
- just assigning them with = ?
- 使用 memcpy 或
- 只是用 = 分配它们?
However in the lower example, I just tested it by allocating memory for two pointers, then assigning the second, changing first..but then the entry of my second pointer is also changing.. What am I doing wrong :/.
然而,在下面的例子中,我只是通过为两个指针分配内存来测试它,然后分配第二个,首先改变......但是我的第二个指针的条目也在改变......我做错了什么:/。
typedef struct {
int a;
int b;
int c;
} my_struct;
int main(int argc, char** argv) {
my_struct* first = malloc(sizeof(my_struct));
first->a = 100; first->b = 101; first->c = 1000;
my_struct* bb = malloc(sizeof(my_struct));
printf("first %d %d %d\n", first->a, first->b, first->c);
bb = first;
printf("second %d %d %d\n", bb->a, first->b, bb->c);
first->a = 55; first->b = 55; first->c = 89;
printf("second %d %d %d\n", bb->a, first->b, bb->c);
}
回答by Anzurio
The moment you do bb = first;, bband firstare pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89;will change the values for a, b, and cin that location. The original value of first, is still lingering in memory but no way to access it anymore.
那一刻你做bb = first;,bb并first都指向相同的内存位置。first->a = 55; first->b = 55; first->c = 89;将改变值a,b以及c在该位置。, 的原始值first仍然在内存中徘徊,但无法再访问它。
I think what you may want to do is *bb = *first;.
我想你可能想做的是*bb = *first;。
回答by PnotNP
Your knowledge about memcpyis correct but you cannot assign contents of "location pointed to by the pointers" just by assigning pointers like you did in your statement mentioned above.
您的知识memcpy是正确的,但是您不能像在上面提到的语句中那样分配指针来分配“指针指向的位置”的内容。
You are assigning one pointer to the another in the following statement:
您在以下语句中将一个指针分配给另一个:
bb = first;
Now both these point to the same memory location (think of bbas an alias to first).
现在这两个都指向同一个内存位置(可以bb看作是 的别名first)。
If you want to copy data then you copy using "data pointed to by pointers" *bb = *first
如果要复制的数据,然后复制使用“通过指针指向的数据”*bb = *first
回答by ex nihilo
As has already been pointed out, if you have a pointer firstthat points to some location in memory, and you make the assignment bb= first, where bbis a compatible pointer type, then bbpoints to the same address as first. This does not copy the contents of the memory referenced by firstto the location originally referenced by bb. It copies the value of the pointer, which is an address, to bb.
正如已经指出的那样,如果您有一个指向first内存中某个位置的指针,并且您进行赋值bb= first,其中bb是兼容的指针类型,则bb指向与first. 这不会将 引用的内存的内容复制first到 最初引用的位置bb。它将指针的值(即地址)复制到bb。
If you define an array A, you can't make the assignment B = Ato copy the contents of Ato B. You must use strcpy()or memcpy()or some such function. But structs are different. You can assign the contents of one struct to a compatible struct.
如果你定义一个数组A,你不能分配B = A给内容复制A到B。您必须使用strcpy()或memcpy()或某些此类功能。但是结构不同。您可以将一个结构的内容分配给兼容的结构。
In your example, bband firstare pointers to structs, and when you write bb = first, now both pointers reference the same address in memory, and you no longer have access to the memory originally referenced by bb-- so now you have a memory leak! But *bband *firstare structs, and when you write *bb = *first, the contents of the struct *firstare copied to the struct *bb. So now you have two different structs, at different locations in memory, each with copies of the same three ints.
在您的示例中,bbandfirst是指向结构的指针,当您写入时bb = first,现在两个指针都引用内存中的相同地址,并且您不再可以访问最初引用的内存bb- 所以现在您有内存泄漏!但是*bband*first是结构体,当你写的时候*bb = *first,结构体的内容*first被复制到结构体中*bb。所以现在你有两个不同的结构,在内存中的不同位置,每个都有相同的三个ints 的副本。
If your my_structtype contained a pointer to int, then after the assignment *bb = *firstthey would each contain a copy of a pointer to the same location in memory, but the data referenced by those pointers would not be copied. So, if the structs contained a pointer to an array, only the pointer would be copied, not the contents of the array, which would be shared by the two structs.
如果您的my_struct类型包含指向 的指针int,则在分配之后,*bb = *first它们每个都将包含指向内存中相同位置的指针的副本,但不会复制这些指针引用的数据。因此,如果结构包含指向数组的指针,则只会复制指针,而不是数组的内容,这将被两个结构共享。

