将char数组赋给文字字符串 - c ++

时间:2021-02-12 21:21:04
char arr[3];
arr="hi";// ERROR
cin>>arr;// and at runtime I type hi, which works fine.

1)can someone explain to me why?

1)有人可以向我解释原因吗?

2)and what's exactly is the type of "hi", I know it's called literal string. but is it just an array of chars too?

2)什么是“hi”的类型,我知道它被称为文字字符串。但它只是一系列的字符吗?

3) isn't cin>>arr; will be just like assign arr to what you type at runtime?

3)不是cin >> arr;就像在运行时键入arr一样?

2 个解决方案

#1


6  

Arrays in C++ are not actual types, just a structured representation of a series of values, and not pointers if you should find that anywhere (they decay into pointers). You can't use them like you would use other types, including assignment. The choice was to either add lots of support for arrays, or to keep them as simple and fast as possible. The latter was chosen, which is one of the distinctions C++ has from some other languages.

C ++中的数组不是实际类型,只是一系列值的结构化表示,而不是指针,如果你应该在任何地方找到它们(它们会衰减成指针)。您不能像使用其他类型一样使用它们,包括分配。选择是为数组添加大量支持,或者尽可能简单快速地保持它们。选择后者,这是C ++与其他语言的区别之一。

To copy an array, copy each element one at a time.

要复制数组,请一次复制一个元素。

In C++11, there is an STL container std::array. It was designed to fit in as a plain array with operator overloading, as well as relating to the rest of the STL.

在C ++ 11中,有一个STL容器std :: array。它被设计成适合作为运算符重载的普通数组,以及与STL的其余部分相关。

A better alternative is std::string. It incorporates the behaviour you want and more, and is specifically designed for holding arrays of characters.

更好的选择是std :: string。它结合了您想要的行为和更多,并且专门用于保存字符数组。

"hi" is, as Konrad Rudolph points out, a const char [3].

正如康拉德鲁道夫所指出的那样,“嗨”是一个常见问题[3]。

As for cining a raw array, it is not possible by standard means because there is no overload provided for cin with arrays. It is possible to create your own overload though. However, I'm not sure how you would account for the different sizes of arrays that get passed unless you define it for a container that knows its size instead of a raw array.

至于对原始数组进行修改,通过标准方法是不可能的,因为没有为带有数组的cin提供过载。但是可以创建自己的重载。但是,我不确定如何解释传递的不同大小的数组,除非您为知道其大小而不是原始数组的容器定义它。

#2


6  

If you'd like, you can declare:

如果您愿意,可以声明:

char array[] = "hi!";

Creates an array and 'initializes' it to 4 bytes long, "hi!"

创建一个数组并将其'初始化'为4个字节长,“嗨!”

char const *array2 = "hey!";

Creates a pointer to read-only memory, a string literal

创建一个指向只读内存的指针,一个字符串文字

array2 = array;

You can now use the array2 pointer to access array one. This is called pointer decay; array and array2 are not of the same type, even though they can cooperate here. An array of type char "decays" to a pointer-to of type char.

您现在可以使用array2指针访问第一个数组。这称为指针衰减; array和array2的类型不同,即使它们可以在这里合作。 char类型的数组“衰减”到char类型的指针。

array = array2; // ERROR

An array is not a pointer. You're thinking like an array is a pointer, when really, it is pre-allocated. You're attempting to assign an address, but array[] already has one "hard-coded" when it was created, and it cannot be changed.

数组不是指针。您认为数组是一个指针,实际上,它是预先分配的。您正在尝试分配一个地址,但是array []在创建时已经有一个“硬编码”,并且无法更改。

#1


6  

Arrays in C++ are not actual types, just a structured representation of a series of values, and not pointers if you should find that anywhere (they decay into pointers). You can't use them like you would use other types, including assignment. The choice was to either add lots of support for arrays, or to keep them as simple and fast as possible. The latter was chosen, which is one of the distinctions C++ has from some other languages.

C ++中的数组不是实际类型,只是一系列值的结构化表示,而不是指针,如果你应该在任何地方找到它们(它们会衰减成指针)。您不能像使用其他类型一样使用它们,包括分配。选择是为数组添加大量支持,或者尽可能简单快速地保持它们。选择后者,这是C ++与其他语言的区别之一。

To copy an array, copy each element one at a time.

要复制数组,请一次复制一个元素。

In C++11, there is an STL container std::array. It was designed to fit in as a plain array with operator overloading, as well as relating to the rest of the STL.

在C ++ 11中,有一个STL容器std :: array。它被设计成适合作为运算符重载的普通数组,以及与STL的其余部分相关。

A better alternative is std::string. It incorporates the behaviour you want and more, and is specifically designed for holding arrays of characters.

更好的选择是std :: string。它结合了您想要的行为和更多,并且专门用于保存字符数组。

"hi" is, as Konrad Rudolph points out, a const char [3].

正如康拉德鲁道夫所指出的那样,“嗨”是一个常见问题[3]。

As for cining a raw array, it is not possible by standard means because there is no overload provided for cin with arrays. It is possible to create your own overload though. However, I'm not sure how you would account for the different sizes of arrays that get passed unless you define it for a container that knows its size instead of a raw array.

至于对原始数组进行修改,通过标准方法是不可能的,因为没有为带有数组的cin提供过载。但是可以创建自己的重载。但是,我不确定如何解释传递的不同大小的数组,除非您为知道其大小而不是原始数组的容器定义它。

#2


6  

If you'd like, you can declare:

如果您愿意,可以声明:

char array[] = "hi!";

Creates an array and 'initializes' it to 4 bytes long, "hi!"

创建一个数组并将其'初始化'为4个字节长,“嗨!”

char const *array2 = "hey!";

Creates a pointer to read-only memory, a string literal

创建一个指向只读内存的指针,一个字符串文字

array2 = array;

You can now use the array2 pointer to access array one. This is called pointer decay; array and array2 are not of the same type, even though they can cooperate here. An array of type char "decays" to a pointer-to of type char.

您现在可以使用array2指针访问第一个数组。这称为指针衰减; array和array2的类型不同,即使它们可以在这里合作。 char类型的数组“衰减”到char类型的指针。

array = array2; // ERROR

An array is not a pointer. You're thinking like an array is a pointer, when really, it is pre-allocated. You're attempting to assign an address, but array[] already has one "hard-coded" when it was created, and it cannot be changed.

数组不是指针。您认为数组是一个指针,实际上,它是预先分配的。您正在尝试分配一个地址,但是array []在创建时已经有一个“硬编码”,并且无法更改。