简体   繁体   English

需要左值作为增量操作数

[英]lvalue required as increment operand

gcc 4.4.4 gcc 4.4.4

What am I doing wrong? 我究竟做错了什么?

char x[10];
char y[] = "Hello";
while(y != NULL)
    *x++ = *y++;

Many thanks for any advice. 非常感谢您的任何建议。

x++ is the short form of x = x + 1 . x++x = x + 1的简称。 However, x here is an array and you cannot modify the address of an array. 但是, x是一个数组,您不能修改数组的地址。 So is the case with your variable y too. 变量y也是如此。

Instead of trying to increment arrays, you can declare an integer i and increment that, then access the i 'th index of an arrays. 您可以声明一个整数i并递增该整数,然后访问数组的第i个索引,而不是尝试递增数组。

char x[10], y[5] = "Hello";
int i = 0;
while (y[i] != 0)
{
    x[i] = *y[i];
    i++;
}
x[i] = 0;

Most likely you fell victim to a popular misconception that "array is a pointer", ie when you define an array what you actually get is an ordinary pointer that points to some block of memory allocated somewhere. 您很可能会成为一个普遍的误解的受害者,即“数组是指针”,即,当您定义数组时,您实际上得到的是一个普通指针,该指针指向某处分配的某些内存块。 In your code you are making an attempt to increment that pointer. 在您的代码中,您正在尝试增加该指针。

The code does not "work" because in reality arrays are not pointers. 该代码不起作用,因为实际上数组不是指针。 Arrays are arrays. 数组是数组。 Arrays cannot be incremented. 数组不能递增。 There's no such operation as "increment an array" in C language. C语言中没有“递增数组”之类的操作。 In fact, arrays by themselves in C are non-modifiable lvalues. 实际上,C语言中的数组本身是不可修改的左值。 There are no operations in C that can modify the array itself (only individual elements can be modifiable). C语言中没有可修改数组本身的操作(只能修改单个元素)。

If you want to traverse your arrays using the "sliding pointer" technique (which is what you are actually trying to do), you need to create the pointers explicitly and make them point to the starting elements of your arrays 如果要使用“滑动指针”技术遍历数组(这实际上是您尝试做的),则需要显式创建指针,并使它们指向数组的起始元素

char *px = x;
char *py = y;

After that you can increment these pointers as much as you want. 之后,您可以根据需要增加这些指针的数量。

Arrays in C are indeed pointers, but constant pointers, which means after declaration their values can't be changed. C语言中的数组确实是指针,但是是常量指针,这意味着声明后不能更改它们的值。

int arr[] = {1, 2, 3};
// arr is declared as const pointer.

(arr + 1) is possible but arr++ is not possible because arr can not store another address since it is constant. (arr + 1)是可能的,但是arr++是不可能的,因为arr因为它是常数而不能存储另一个地址。

char x[10];
char y[] = "Hello";
char *p_x = &x[0];
char *p_y = &y[0];
while(*p_y != '\0') *p_x++ = *p_y++;

Since you can't modify the array addresses (done by x++ and y++ in your code) and you can modify the pointer address, I copied over the address of the array into separate pointers and then incremented them. 由于您不能修改数组地址(在代码中由x++y++完成),并且可以修改指针地址,因此我将数组的地址复制到单独的指针中, 然后对其进行了递增。

If you want, I'm sure you can reduce the notation, but I hope you got the point. 如果您愿意,我敢肯定您可以减少表示法,但我希望您明白这一点。

x and y are arrays, not pointers. xy是数组,而不是指针。

They decay into pointers in most expression contexts, such as your increment expression, but they decay into rvalues, not lvalues and you can only apply increment operators to lvalues. 它们在大多数表达式上下文(例如您的增量表达式)中衰减为指针,但它们衰减为rvalues,而不是lvalues,并且您只能将增量运算符应用于lvalues。

Since you've defined both x and y as arrays, you can't modify them. 由于您已将xy都定义为数组,因此无法修改它们。 One possibility would be to use pointers instead: 一种可能性是改用指针:

char x[10];
char *xx = x;

char *y = "Hello";

while (*y != '\0')
    *xx++ = *y++;

Note that I've also fixed your termination condition -- a pointer won't become NULL just because it's reached the end of a string. 请注意,我还修复了您的终止条件-指针不会仅仅因为它已到达字符串的末尾而变为NULL

At most times, array just like a pointer. 在大多数时候,数组就像一个指针。

Just remember you can't modify array! 只记得您不能修改数组!

And y++ is y = y + 1 . y++y = y + 1

char y[] = "Hello";

So you do modify array when you y++ !! 因此,当您使用y++时,确实要修改数组!

It will produce error: lvalue required as increment operand . 它将产生error: lvalue required as increment operand

We can not modify a array name, but What about argv++ in f(int argv[]) ? 我们无法修改数组名称,但是f(int argv[]) argv++呢?

Quotes from K&R in p99 “an array name is not a varible; K&R在第99页中引述“数组名称不是变量; construction like a = pa and a++ are illegal" which says the name of an array is a synonym for the location of the initial element.” a = paa++是非法的”,它表示数组的名称是初始元素位置的同义词。”

But why in function parameter func(char *argv[]) , we can do argv++ despite of argv is a array name. 但是为什么在函数参数func(char *argv[]) ,尽管argv是一个数组名,我们仍然可以执行argv++

And in int *a[10] , we can't do the a++ like argv++ . int *a[10] ,我们不能像argv++那样做a++ argv++

The name of array is a synonym for the location of the initial element. 数组的名称是初始元素位置的同义词。 ---K&R --- K&R

arrayname++ is illegal. arrayname++是非法的。

In function parameter, such as char *argv[] , it is the same as char **argv . 在函数参数中,例如char *argv[] ,与char **argv相同。 type *arrayname_para[] in parameter is a another synonym for type **arrayname_para . 参数中的type *arrayname_para[] type **arrayname_para的另一个同义词。

Arrays are constant pointers. 数组是常量指针。 We can't change them. 我们无法更改它们。

int q;
int *const p = &q;
p = NULL; // this is not allowed.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM