C++ 为字符数组赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14915924/
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
Assigning value to char array
提问by Rutvij Kotecha
I get no error when I type
我输入时没有错误
char a[10] = "Hi";
But when I change it to the following, I get an error: Array type char is not assignable.
但是当我将其更改为以下内容时,出现错误: Array type char is not assignable.
char a[10];
a = "Hi";
Why is array type char not assignable? Is it because the language is written that way on purpose or am I missing a point?
为什么数组类型 char 不可分配?是因为语言是故意这样写的还是我错过了一点?
采纳答案by Cogwheel
The C++ way of doing this, as I commented above, would be to use std::string
instead of char[]
. That will give you the assignment behavior you're expecting.
正如我在上面评论的那样,这样做的 C++ 方法是使用std::string
而不是char[]
. 这将为您提供您期望的分配行为。
That said, the reason you're only getting an error for the second case is that the =
in these two lines mean different things:
也就是说,您只在第二种情况下收到错误的原因是=
这两行中的意思不同:
char a[10] = "Hi";
a = "Hi";
The first is an initialization, the second is an assignment.
第一个是初始化,第二个是赋值。
The first line allocates enough space on the stack to hold 10 characters, and initializes the first three of those characters to be 'H', 'i', and '\0'. From this point on, all a
does is refer to the position of the the array on the stack. Because the array is just a place on the stack, a
is never allowed to change. If you want a different location on the stack to hold a different value, you need a different variable.
第一行在堆栈上分配足够的空间来容纳 10 个字符,并将这些字符的前三个初始化为 'H'、'i' 和 '\0'。从这点开始,a
所做的就是引用数组在堆栈上的位置。因为数组只是栈上的一个地方,a
是永远不允许改变的。如果您希望堆栈上的不同位置保存不同的值,则需要不同的变量。
The second (invalid) line, on the other hand, tries to change a
to refer to a (technically different) incantation of "Hi"
. That's not allowed for the reasons stated above. Once you have an initialized array, the only thing you can do with it is read values from it and write values to it. You can't change its location or size. That's what an assignment would try to do in this case.
另一方面,第二行(无效)尝试更改a
以引用(技术上不同的)咒语"Hi"
。由于上述原因,这是不允许的。一旦你有了一个初始化的数组,你唯一能做的就是从中读取值并向其中写入值。您无法更改其位置或大小。这就是任务在这种情况下会尝试做的事情。
回答by learnvst
An array is not a modifiable lvalue
数组不是可修改的左值
use
用
char a[10];
strcpy(a, "Hi");
回答by NPE
The language does not allow assigning string literals to character arrays. You should use strcpy()
instead:
该语言不允许将字符串文字分配给字符数组。你应该使用strcpy()
:
strcpy(a, "Hi");
回答by NPE
a is a pointer to the array, not the array itself. It cannot be reassigned.
a 是指向数组的指针,而不是数组本身。它不能重新分配。
You tagged with C++ BTW. For that case better use std::string. It's probably more what you're expecting.
你用 C++ BTW 标记。对于这种情况,最好使用 std::string。这可能更符合您的期望。
回答by SGH
Simple, the
简单的,
char a[10] = "Hi";
is a little "extra feature", as it cannot be done like that on run-time.
是一个小“额外功能”,因为它不能像运行时那样完成。
But that's the reason for C/C++ standard libraries.
但这就是 C/C++ 标准库的原因。
#include <string.h>
// ...
strcpy(a,"Test"); // STR-ing C-o-PY.
This comes from the C's standard library. If using C++ you should use std::string, unless you really want to suck all the possible performance from your destination PC.
这来自 C 的标准库。如果使用 C++,你应该使用 std::string,除非你真的想从你的目标 PC 上吸取所有可能的性能。
回答by Hayri U?ur Koltuk
this is because initialization is not an assignment. the first thing which works is an initialization, and the second one, which does not work, as expected, is assignment. you simply cant assign values to arrays you should use sth like strcpy
or memcpy
. or you can alternatively use std::copy
from <algorithm>
这是因为初始化不是赋值。第一件事是初始化,第二件事,正如预期的那样不起作用,是赋值。您根本无法将值分配给您应该使用之类的数组strcpy
或memcpy
。或者您也可以使用std::copy
from<algorithm>
回答by Muhammad Shujauddin
It is so simple,(=) have two different mean assignment and initialization. You can also write your code like that
就这么简单,(=) 有两种不同的均值赋值和初始化。你也可以这样写你的代码
#include <iostream>
using namespace std;
int main ()
{
char message[3] = {'H', 'i', '##代码##'};
cout << message<< endl;
return 0;
}
in this code you have no need to write a difficult code or function and even no need of string.h
在此代码中,您无需编写困难的代码或函数,甚至无需 string.h