C语言 c 指针和数组,将内容复制到另一个数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22976316/
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
c pointers and arrays, copying contents into another array
提问by cHam
I know this is probably a basic question, but i've never fully grasped the whole pointers concept in C.
我知道这可能是一个基本问题,但我从未完全掌握 C 中的整个指针概念。
My question is, say i have an int array and I pass that into a function like `
我的问题是,假设我有一个 int 数组,我将它传递给一个像 `
int main(){
int *a = malloc(sizeof(int*)*4);
// add values to a
p(a);
}
void p(int *a){
int *b = malloc(sizeof(int*)*4)
b = a;
// change values in b
//print a b
}`
What is the correct way to do this so that whatever changes I make to b do not affect the values in a?
这样做的正确方法是什么,以便我对 b 所做的任何更改都不会影响 a 中的值?
采纳答案by Brandon Haston
In your 'p' method, you're assigning pointer b to be pointer a, or, in otherwords, you're making b point to what a points to. Any changes to b will cause changes to a since they both wind up pointing to the same block of memory.
在您的“p”方法中,您将指针 b 分配为指针 a,或者换句话说,您让 b 指向 a 指向的内容。对 b 的任何更改都会导致对 a 的更改,因为它们最终都指向同一个内存块。
Use memcpy to copy blocks of memory. It would look something like this:
使用 memcpy 复制内存块。它看起来像这样:
#include <string.h>
#include <stdlib.h>
void p(int *a){
int *b = (int*)malloc(sizeof(int)*4);
memcpy(b, a, sizeof(int)*4);
//make changes to b.
b[0] = 6;
b[1] = 7;
b[2] = 8;
b[3] = 9;
}
int main(int argc, char **argv)
{
int *a = (int*)malloc(sizeof(int)*4);
// add values to a
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
p(a);
return 0;
}
回答by John3136
Just assigning the pointer means bis pointing to the same chunk of memory as aand the memory you just allocated "for b" has leaked. It's allocated but you can't free it any more.
仅分配指针意味着b指向相同的内存块,a并且您刚刚“为b”分配的内存已泄漏。它已分配,但您不能再释放它。
To copy the array you need to well, copy it.
要复制您需要的数组,请复制它。
Easy way is lookup the various memcpymethods, or just do it the longer way
简单的方法是查找各种memcpy方法,或者只是使用更长的方法
for (int i = 0; i < 4; i++) {
b[i] = a[i];
}
You need to know about "shallow copy" and "deep copy" - since you have an array of int*, what I put above is a shallow copy. If you need to copy the contents that each int*points to, you'll need something more complex again.
你需要了解“浅拷贝”和“深拷贝”——因为你有一个 . 数组int*,我上面放的是一个浅拷贝。如果您需要复制每个int*指向的内容,您将再次需要更复杂的东西。
回答by Ivan Aksamentov - Drop
回答by Manuel
void p(int *a){
int *b = malloc(sizeof(int*)*4)
int size=4;
while(size>=0){
b[size--]=a[size--];
}
// change values in b
//print a b
}
This should work!
这应该有效!
回答by brokenfoot
You are pointing aand bto two different blocks of memory and then assigning bto the block pointed to by a, causing a memory leak.
您指向a和b两个不同的内存块,然后分配给b指向的块a,导致内存泄漏。
And since you are not returning anything from your function p(), you can allocate bon stack (I wonder what you are doing with it).
而且由于您没有从函数中返回任何内容p(),因此您可以b在堆栈上分配(我想知道您在用它做什么)。
If your intention is to copy the data pointed to by these pointers, you can use memcpyor copy element by element as others have suggested.
如果您打算复制这些指针指向的数据,您可以memcpy像其他人建议的那样逐个使用或复制元素。

