开发者

Question on equating arrays in C

开发者 https://www.devze.com 2023-01-16 18:26 出处:网络
Suppose I have two int arrays x and y, and both of them have length 3. x={0,1,2}. Is there any one step way to assign the values of x to y. W开发者_如何学Gohen I do y=x, and try to print the values

Suppose I have two int arrays x and y, and both of them have length 3. x={0,1,2}.

Is there any one step way to assign the values of x to y. W开发者_如何学Gohen I do y=x, and try to print the values of y,

the code does not compile.

I dont want to go through the pain of writing a for loop, and write y[i]=x[i]


You can use memcpy or memmove:

memcpy(y, x, sizeof x);


Although you can't do this:

int x[ 3 ] = { 0, 1, 2 };  // an array of three items
int y[ 3 ];                // another array of three items

y = x;                     // compile error - y isn't a pointer that can be reassigned.

You can do this:

int x[ 3 ] = { 0, 1, 2 };
int *y = x;   // a pointer to an array

You're creating y as a pointer to the array x, rather than a copy of x. If you change the values of x, those changes will also appear when referencing y.


You need to loop.

These are essentially fixed pointers, so you can't just assign one to another. You have to copy each value.

edit

Here's why I don't recommend memcpy:

  1. It's not faster for any small number of elements; it will likely be slower.

  2. The version of memcpy he's using does not include any safeguards to prevent buffer overrun. It's very easy, even for non-novices, to confuse the number of elements with the total size of the elements, leading to a serious bug. It's less easy to mess up a for loop.

  3. Since this is just C, you can (usually) safely memcpy an element. If this were C++, you would want to ensure that the copy constructor was invoked. This makes the use of memcpy a bad habit.

In direct response to Carl:

  1. Yes, memcpy can be very fast for large arrays of simple data. This is not what we're dealing with here, though.

  2. I'm not concerned about bugs in memcpy, but the ease of having a bug in using it. It requires the count of bytes, but the natural thing would be the count of elements. A for loop would use the element count. On many platforms, there are safer versions that infer the buffer size and prevent overwrites; I'd recommend one of those, if possible, when memcpy is a good idea.

  3. If this were an array of C++ objects, such as std::string, then using memcpy instead of invoking the copy constructor would be a bug in itself. Yes, I do realize that the example is C, not C++, but that doesn't make it a good idea.


If two or more variables are declared as the same type of struct, and the definition of the struct contains arrays, the variables can be assigned to each other and the arrays will be copied in their entirety. Sometimes it may be handy to create a struct containing a single array, for precisely that purpose.


Roll them up in a struct (or union, in this case).

struct int3 {
    int A[3];
};

int3 x = {{ 0, 1, 2 }};
int3 y = x;

This will not generally result in better code from the compiler than doing individual copies of each element or using memcpy, and may result in worse code.


You need to loop or to call a library function that does that for you, e.g. memcpy/memmove.


There is no way built into the language. You can probably find a library call to do it, though. If it's something you're doing frequently, you could easily write the function yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消