错误:不能增加类型“char[6]”的值[复制]

时间:2022-07-27 22:31:02

This question already has an answer here:

这个问题已经有了答案:

I am trying to learn pointers and string literals in C/C++. As per my understanding, string literals are char arrays with a null \0 at the end. Also we can basically do all the pointer arithmetic operations on array like increment and decrement.

我试着学习C/ c++中的指针和字符串。根据我的理解,字符串字面量是字符数组,最后是零\0。我们还可以在数组上做所有的指针算术运算,比如递增和递减。

But when I am trying to run the following code:

但是当我尝试运行以下代码时:

#include <iostream> 
using namespace std;

int main (){
    char *c ="Hello";
    char d[6];

    while(*d++ = *c++);

    cout<<c<<endl<<d;
} 

I am getting the following error,

我得到了如下错误,

error: cannot increment value of type 'char [6]'
    while(*d++ = *c++);

My assumption for this code was, that the values of string literal c will be copied to char array d.

我对这个代码的假设是,字符串文字c的值将被复制到char数组d。

Edit: Now I am a bit confused about the difference between these 2 statements:

编辑:现在我有点搞不清楚这两个声明的区别:

*(d++)

and

*(d+1)

assuming d is an array.

假设d是一个数组。

2 个解决方案

#1


4  

char d[6];
while(*d++ = *c++);

Should be Re-written to:

应该重写:

char d[6];
int idx = 0;
while(d[idx++] = *c++);

Because in char d[6];, d is an array (not to be confused with pointer) and you can not change the address of an array. On the other hand, type of c is char * so you can modify it.

因为在char d[6]中,d是一个数组(不要与指针混淆),您不能更改数组的地址。另一方面,c的类型是char *,所以您可以修改它。

c++; // OK
c = &d[0]; // OK

d++; // Not allowed
d = c; // Not allowed

About your added question:

关于你说的问题:

Difference between: *(d++) and *(d+1)

区别:*(d++)和*(d+1)

Consider following example:

考虑下面的例子:

int index1 = 42, index2 = 42;
int j = index1++; // j = 42 and index1 = 43
int k = (index2 + 1); // k = 43 and index2 = 42 (unchanged)

Similarly when you write *(d++), you are trying to access (by dereferencing) the current location and then increment the pointer itself to next location. But when you write *(d + 1), you are accessing the next location and the pointer itself remains unchanged.

同样,当您编写*(d++)时,您试图访问(通过取消引用)当前位置,然后将指针本身增加到下一个位置。但是当您写入*(d + 1)时,您正在访问下一个位置,指针本身保持不变。

If the d pointer is constant or is an array first form (where d is changed) is not allowed but the second form (where the pointer itself remains unchanged) is allowed.

如果d指针是常量,或者是数组的第一种形式(其中d是更改的),则允许使用第二种形式(指针本身保持不变)。

#2


1  

Array name can't be a modifiable lvalue

数组名不能是可修改的lvalue。

so

所以

d++

can be written as

可以写成

d = d+1;

So there is an error which should be fixed as

所以有一个错误应该是固定的。

while(d[index++] = *c++);

#1


4  

char d[6];
while(*d++ = *c++);

Should be Re-written to:

应该重写:

char d[6];
int idx = 0;
while(d[idx++] = *c++);

Because in char d[6];, d is an array (not to be confused with pointer) and you can not change the address of an array. On the other hand, type of c is char * so you can modify it.

因为在char d[6]中,d是一个数组(不要与指针混淆),您不能更改数组的地址。另一方面,c的类型是char *,所以您可以修改它。

c++; // OK
c = &d[0]; // OK

d++; // Not allowed
d = c; // Not allowed

About your added question:

关于你说的问题:

Difference between: *(d++) and *(d+1)

区别:*(d++)和*(d+1)

Consider following example:

考虑下面的例子:

int index1 = 42, index2 = 42;
int j = index1++; // j = 42 and index1 = 43
int k = (index2 + 1); // k = 43 and index2 = 42 (unchanged)

Similarly when you write *(d++), you are trying to access (by dereferencing) the current location and then increment the pointer itself to next location. But when you write *(d + 1), you are accessing the next location and the pointer itself remains unchanged.

同样,当您编写*(d++)时,您试图访问(通过取消引用)当前位置,然后将指针本身增加到下一个位置。但是当您写入*(d + 1)时,您正在访问下一个位置,指针本身保持不变。

If the d pointer is constant or is an array first form (where d is changed) is not allowed but the second form (where the pointer itself remains unchanged) is allowed.

如果d指针是常量,或者是数组的第一种形式(其中d是更改的),则允许使用第二种形式(指针本身保持不变)。

#2


1  

Array name can't be a modifiable lvalue

数组名不能是可修改的lvalue。

so

所以

d++

can be written as

可以写成

d = d+1;

So there is an error which should be fixed as

所以有一个错误应该是固定的。

while(d[index++] = *c++);