C语言 C 中的字符数组声明和初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4978056/
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
Char array declaration and initialization in C
提问by Alexey Pimenov
I was curious about why this is not allowed in C:
我很好奇为什么在 C 中不允许这样做:
char myarray[4];
myarray = "abc";
And this is allowed:
这是允许的:
char myarray[4] = "abc";
I know that in the first case I should use strcpy:
我知道在第一种情况下我应该使用strcpy:
char myarray[4];
strcpy(myarray, "abc");
But why declaration and later initialization is not allowed and declaration and simultaneous initialization is allowed? Does it relate to memory mapping of C programs?
但是为什么不允许声明和稍后初始化而允许声明和同时初始化?它与 C 程序的内存映射有关吗?
Thanks!
谢谢!
回答by Frédéric Hamidi
That's because your first code snippet is not performing initialization, but assignment:
那是因为您的第一个代码片段没有执行初始化,而是执行:
char myarray[4] = "abc"; // Initialization.
myarray = "abc"; // Assignment.
And arrays are not directly assignable in C.
数组在 C 中不能直接赋值。
The name myarrayactually resolves to the address of its first element (&myarray[0]), which is not an lvalue, and as such cannot be the target of an assignment.
该名称myarray实际上解析为其第一个元素 ( &myarray[0])的地址,该地址不是左值,因此不能成为赋值的目标。
回答by Vlad
Yes, this is a kind of inconsistency in the language.
是的,这是一种语言上的不一致。
The "=" in myarray = "abc";is assignment (which won't work as the array is basically a kind of constant pointer), whereas in char myarray[4] = "abc";it's an initialization of the array. There's no way for "late initialization".
"=" inmyarray = "abc";是赋值(它不起作用,因为数组基本上是一种常量指针),而char myarray[4] = "abc";它是数组的初始化。没有办法“延迟初始化”。
You should just remember this rule.
你应该记住这个规则。
回答by davep
This is another C example of where the same syntax has different meanings (in different places). While one might be able to argue that the syntax should be different for these two cases, it is what it is. The idea is that not that it is "not allowed" but that the second thing means something different (it means "pointer assignment").
这是另一个 C 示例,其中相同的语法具有不同的含义(在不同的地方)。虽然人们可能会争辩说这两种情况的语法应该不同,但事实就是如此。这个想法不是说它是“不允许的”,而是第二件事意味着不同的东西(它的意思是“指针分配”)。
回答by pierre
myarray = "abc";
...is the assignation of a pointer on "abc" to the pointer myarray.
...是将“abc”上的指针分配给指针 myarray。
This is NOT filling the myarray buffer with "abc".
这不是用“abc”填充 myarray 缓冲区。
If you want to fill the myarray buffer manually, without strcpy(), you can use:
如果要手动填充 myarray 缓冲区,而无需 strcpy(),则可以使用:
myarray[0] = 'a', myarray[1] = 'b', myarray[2] = 'c', myarray[3] = 0;
or
或者
char *ptr = myarray;
*ptr++ = 'a', *ptr++ = 'b', *ptr++ = 'c', *ptr = 0;
Your question is about the difference between a pointer and a buffer (an array). I hope you now understand how C addresses each kind.
您的问题是关于指针和缓冲区(数组)之间的区别。我希望您现在了解 C 如何处理每种类型。
回答by Nofate
I think these are two really different cases. In the first case memory is allocated and initialized in compile-time. In the second - in runtime.
我认为这是两个完全不同的案例。在第一种情况下,内存是在编译时分配和初始化的。在第二个 - 在运行时。

