简体   繁体   English

c指针和数组,将内容复制到另一个数组中

[英]c pointers and arrays, copying contents into another array

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中的值?

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. 在你的'p'方法中,你将指针b指定为指针a,或者换句话说,你指的是b指向指向的指针。 Any changes to b will cause changes to a since they both wind up pointing to the same block of memory. 对b的任何更改都会导致对a的更改,因为它们都会指向同一块内存。

Use memcpy to copy blocks of memory. 使用memcpy复制内存块。 It would look something like this: 它看起来像这样:

#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;
}

Just assigning the pointer means b is pointing to the same chunk of memory as a and the memory you just allocated "for b " has leaked. 刚分配的指针指b指向相同的内存块作为a和你只是分配“的内存b ”已泄漏。 It's allocated but you can't free it any more. 它被分配但你不能再释放它了。

To copy the array you need to well, copy it. 要复制您需要的数组,请复制它。

Easy way is lookup the various memcpy methods, 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. 你需要知道“浅拷贝”和“深拷贝” - 因为你有一个int*数组,我上面提到的是浅拷贝。 If you need to copy the contents that each int* points to, you'll need something more complex again. 如果你需要复制每个int*指向的内容,你将需要一些更复杂的东西。

You are pointing a and b to two different blocks of memory and then assigning b to the block pointed to by a , causing a memory leak. 您指向ab两个存储器的不同块,然后分配b到块指向a ,引起了内存泄漏。

And since you are not returning anything from your function p() , you can allocate b on 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 memcpy or copy element by element as others have suggested. 如果您打算复制这些指针指向的数据,您可以像其他人建议的那样使用memcpy或逐个元素复制。

C does not support array assignment! C不支持数组赋值! You must copy data manually. 您必须手动复制数据。

  • Use memcpy 使用memcpy
  • Use memmove if a and b (could) overlap 如果a和b(可能)重叠,请使用memmove
  • Use for or while loop 使用forwhile循环
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! 这应该工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM