C语言 为什么我不能将数组分配为 &a = &b?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7882735/
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
Why can't I assign arrays as &a = &b?
提问by Starfighter911
I have a problem assigning an array like:
我在分配一个数组时遇到问题:
int a[];
int b[] = {1,2,3};
&a = &b;
I know I could use pointers but I want to try it that way...
我知道我可以使用指针,但我想这样尝试......
回答by caf
You can't assign arrays in C. You can copy them with the memcpy()function, declared in <string.h>:
您不能在 C 中分配数组。您可以使用在 中memcpy()声明的函数复制它们<string.h>:
int a[3];
int b[] = {1,2,3};
memcpy(&a, &b, sizeof a);
回答by Oliver Charlesworth
That way doesn't work, as you have found. You cannot assign arrays in C.
正如您所发现的那样,这种方式不起作用。你不能在 C 中分配数组。
Structs, however, are assignable. So you can do this:
但是,结构是可分配的。所以你可以这样做:
typedef struct
{
int x[3];
} T;
T a;
T b = { { 1, 2, 3 } };
a = b;

