C++ 将字符串分配给 char 数组

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

Assigning a string of characters to a char array

c++

提问by Iqbal Khan

I Want to know why the first statements works and why not second one in c++

我想知道为什么第一个语句在 C++ 中起作用,为什么第二个语句不起作用

char a[10]="iqbal";  // it works

a="iqbal"; // does not work 

回答by Pavan Manjunath

Strictly speaking, an array is not a pointer! And an array ( base address of the array ) cant be a modifiable lvalue. ie it cannot appear on the left hand side of an assignment operator.Arrays decay into pointers only in certain circumstances. Read this SO postto learn when arrays decay into pointers. Here is one more nice articlewhich explains the differences between arrays and pointers

严格来说,数组不是指针!并且数组(数组的基地址)不能是可修改的左值。即它不能出现在赋值运算符的左侧。只有在某些情况下,数组才会衰减为指针。阅读这篇SO 帖子以了解数组何时衰减为指针。这是另一篇很好的文章,它解释了数组和指针之间的区别

Also read about lvalues and rvalues hereso that you get an idea of things which cannot appear on the LHS of =

还可以在这里阅读左值和右值以便您了解不能出现在 LHS 上的事物=

char a[10]="iqbal"; ?// it works

字符 a[10]="iqbal"; ?// 有用

In this case, internally what happens is

在这种情况下,内部发生的事情是

a[0] = 'i';
a[1] = 'q'; 
 .
 .
a[5] = '
0x60000(Address of a, but is a simple number here ) = Address of "iqbal"
';

So everything is fine as array[i]is a modifiable lvalue.

所以一切都很好,array[i]可修改的左值也是如此。

a="iqbal"; // does not work

一个=“iqbal”;// 不起作用

Internally, this is roughly equivalent to

在内部,这大致相当于

char a[10];
strncpy(a, "iqbal", sizeof(a) - 1);
a[sizeof(a) - 1] = 0;

This is wrong as we cannot assign something to a number.

这是错误的,因为我们不能为一个数字分配一些东西。

回答by Albert Veli

The char array a will be static and can not be changed if you initialize it like this. Anyway you can never assign a character string a="iqbal" in c. You have to use strncpy or memcpy for that. Otherwise you will try to overwrite the pointer to the string, and that is not what you want.

char 数组 a 将是静态的,如果您像这样初始化它,则无法更改。无论如何,您永远不能在 c 中分配字符串 a="iqbal" 。为此,您必须使用 strncpy 或 memcpy。否则,您将尝试覆盖指向字符串的指针,这不是您想要的。

So the correct code would do something like:

所以正确的代码会做这样的事情:

std::string a = "iqbal";  // it works
a="iqbal"; // so does this

The -1 is to reserve a byte for the terminating zero. Note, you will have to check for yourself if the string is null terminated or not. Bad api. There is a strlcpy() call that does this for you but it is not included in glibc.

-1 是为终止零保留一个字节。请注意,您必须自己检查字符串是否以空字符结尾。糟糕的 API。有一个 strlcpy() 调用可以为您执行此操作,但它不包含在 glibc 中。

回答by ouah

The first line is not a statement but a declaration with an initialization. The second line is an expression statement with the assignment operator.

第一行不是语句而是带有初始化的声明。第二行是带有赋值运算符的表达式语句。

You cannot assign arrays in C.

你不能在 C 中分配数组。

But you can initialize an array with the elements of a string literal.

但是您可以使用字符串文字的元素初始化数组。

回答by Rob?

why the first statements works and why not second one in c++

为什么第一个语句在 C++ 中起作用,为什么第二个语句不起作用

Because they are different statements, almost wholly unrelated. Do not be confused by the fact that they both use the =symbol. In one case, it represents object initialization. In the other case, the assignment operator.

因为它们是不同的陈述,几乎完全不相关。不要被他们都使用这个=符号的事实弄糊涂了。在一种情况下,它代表对象初始化。在另一种情况下,赋值运算符。

Your first line is legal because it is legal to initialize aggregates, including character arrays.

您的第一行是合法的,因为初始化聚合(包括字符数组)是合法的。

Your second line is not legal because it is not legal to assign to an array.

您的第二行不合法,因为分配给数组是不合法的。

Since this is C++, may I suggest that you avoid naked arrays? For character strings use std::string. For other arrays use std::vector. If you do, you example becomes:

由于这是 C++,我可以建议您避免使用裸数组吗?对于字符串使用std::string. 对于其他数组,请使用std::vector. 如果你这样做,你的例子变成:

char a[10]="iqbal";
char *my_a = a;

回答by ydk2

try:

尝试:

##代码##

and work with my_a.

并与 my_a 一起工作。

回答by nims

When writing char a[10]="iqbal" You are initializing the elements of the character array awith the characters. We can do the same with inttype (note that the chartype gets a slightly different treatment) : int a[10]={1,2,...};

写入时 char a[10]="iqbal" 您正在使用字符初始化字符数组a的元素。我们可以对int类型做同样的事情(注意char类型的处理略有不同): int a[10]={1,2,...};

But writing the following after declaration part would be invalid as awould be treated just like a pointer. So writing something like a={1,2,...}; or a="iqbal" won't be making any sense!

但是在声明部分之后编写以下内容将是无效的,因为a将被视为指针。所以写一些像 a={1,2,...}; 或 a="iqbal" 将没有任何意义!