C语言 为什么可以将字符串分配给 char* 指针,而不能分配给 char[] 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6803387/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:12:34  来源:igfitidea点击:

Why can a string be assigned to a char* pointer, but not to a char[] array?

cstringpointersinitializationreusability

提问by nick

Can someone explain why this works with the pointer:

有人可以解释为什么这适用于指针:

char * str1;

str1 = "Hello1";

str1 = "new string";

// but not this
char str2 [] = "hello";
str2 = "four";

// or this
char str3 [];
str3 = "hello";
str3 = "hello";

回答by tryurbest

Why it works with pointers:
When you say char * str1in C, you are allocating a pointer in the memory. When you write str1 = "Hello";, you are creating a string literal in memory and making the pointer point to it. When you create another string literal "new string"and assign it to str1, all you are doing is changing where the pointer points.

为什么它与指针一起工作:
当您char * str1在 C 中说时,您正在内存中分配一个指针。当您编写时str1 = "Hello";,您正在内存中创建一个字符串文字并使指针指向它。当您创建另一个字符串文字"new string"并将其分配给 时str1,您所做的就是更改指针指向的位置。

Why it doesn't work with arrays:
When you say char str2 [] = "Hello", you are creating a string literal and putting it in the array during its definition. It is ok to not give a size, as the array calculates it and appends a '\0'to it. You cannot reassign anything to that array without resizing it. That is why str2 = "four"will not work.

为什么它不适用于数组:
当您说 时char str2 [] = "Hello",您正在创建一个字符串文字并在其定义期间将其放入数组中。不给出大小是可以的,因为数组会计算它并将 a 附加'\0'到它。您不能在不调整数组大小的情况下将任何内容重新分配给该数组。这就是为什么str2 = "four"行不通。

In case of str3, it is the same case. You haven't defined the size of the array in the definition, so it calculated its size to be 0. You cannot assign anything new without resizing the array.

在 的情况下str3,情况相同。你没有在定义中定义数组的大小,所以它计算出它的大小为 0。你不能在不调整数组大小的情况下分配任何新的东西。

回答by Armen Tsirunyan

An array and a pointer are different things, that's why. You can assign to a pointer, but you can't assign to an array. A special exception is made for initialization of char arrays with string literals.

数组和指针是不同的东西,这就是原因。您可以分配给指针,但不能分配给数组。一个特殊的例外是用字符串文字初始化 char 数组。

char a[] = "Hello"; //initialize a char array with string literal. Special case, OK
char* p = "Hello"; //initializa a pointer with an array(which gets converted to pointer)
p = "My";   //assign pointer to point to another value. OK
a = "My";   //error, arrays cannot be assigned to. Use `strcpy`

String literals (such as "Hello") have type char[N]where Nis number of characters (including the terminating '\0'). An array can be converted to a pointer to its first element, but arrays and pointers are not the same thing, whatever some bad books or teachers may say.

字符串文字(例如“Hello”)的类型为char[N]whereN是字符数(包括终止符'\0')。数组可以转换为指向它的第一个元素的指针,但数组和指针不是一回事,不管一些坏书或老师可能会说什么。

回答by Julian

Put simply, because an array is not a first-class object in C/C++. The only way to assign to an array is to use str(n)cpy or memcpy.

简单地说,因为数组不是 C/C++ 中的一流对象。分配给数组的唯一方法是使用 str(n)cpy 或 memcpy。

While an array collapses into a pointer when passed to a function, it is not possible to assign to an array, except at compile-time as initialisation.

虽然数组在传递给函数时会折叠为指针,但不能分配给数组,除非在编译时作为初始化。

回答by Sawan

It is simply because, when you write this code:

这仅仅是因为,当您编写此代码时:

char str2 [] = "hello";

or even:

甚至:

int arr[] = {1,2,4,4,5};

it creates str2or arras a constant pointer. That's why you can not reassign any other values to these pointers while in later case you are creating a normal pointer and you can assign anything to it.

它创建str2arr作为一个常量指针。这就是为什么您不能为这些指针重新分配任何其他值的原因,而在稍后的情况下,您正在创建一个普通指针并且您可以为其分配任何内容。

回答by joker007

The case with pointersIt works because when you are assigning like str1="Hello", You are actually creating a string literal named hello allocating it somewhere in the memory , and assigning the address of first character of the literal to the pointer , and as the pointer is not constant you can assign it again with different addresses. And one more important point to note is that the string literal created are in read only memory.

指针的情况它之所以有效是因为当您分配 like 时str1="Hello",您实际上是在创建一个名为 hello 的字符串文字,将其分配在内存中的某个位置,并将文字的第一个字符的地址分配给指针,因为指针不是常量您可以使用不同的地址再次分配它。需要注意的更重要的一点是创建的字符串文字位于只读内存中。

The case with character arrayYou can assign it a string literal while initialisation as that is supported by the language . And dont confuse assignment with initialisation. While assignment , since its an character array you have to change value character by character ,You are trying to address the first address of the string literal to the first character of the array ( the name of the array return the address of first element of the array).And this clearly is not right as the first element is not pointer , it cant store address.

字符数组的情况您可以在初始化时为其分配一个字符串文字,因为语言支持。并且不要将赋值与初始化混淆。在赋值时,由于它是一个字符数组,您必须逐个字符地更改值,您正在尝试将字符串文字的第一个地址寻址到数组的第一个字符(数组的名称返回数组的第一个元素的地址)数组)。这显然是不对的,因为第一个元素不是指针,它不能存储地址。