C语言 需要左值作为增量操作数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3364445/
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
lvalue required as increment operand
提问by ant2009
gcc 4.4.4
海湾合作委员会 4.4.4
What am I doing wrong?
我究竟做错了什么?
char x[10];
char y[] = "Hello";
while(y != NULL)
*x++ = *y++;
Many thanks for any advice.
非常感谢您的任何建议。
回答by naivnomore
x++is the short form of x = x + 1. However, xhere is an array and you cannot modify the address of an array. So is the case with your variable ytoo.
x++是 的缩写形式x = x + 1。但是,x这是一个数组,您不能修改数组的地址。您的变量y也是如此。
Instead of trying to increment arrays, you can declare an integer iand 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;
回答by AnT
Most likely you fell victim to a popular misconception that "array is a pointer", i.e. 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. In fact, arrays by themselves in C are non-modifiablelvalues. There are no operations in C that can modify the array itself (only individual elements can be modifiable).
代码不能“工作”,因为实际上数组不是指针。数组就是数组。数组不能递增。C语言中没有“增加数组”这样的操作。事实上,C 中的数组本身是不可修改的左值。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.
之后,您可以根据需要增加这些指针。
回答by Sushil Kadu
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 arrcan not store another address since it is constant.
(arr + 1)是可能的,但arr++不可能,因为arr不能存储另一个地址,因为它是常量。
回答by Jacob
char x[10];
char y[] = "Hello";
char *p_x = &x[0];
char *p_y = &y[0];
while(*p_y != 'char x[10];
char *xx = x;
char *y = "Hello";
while (*y != 'char y[] = "Hello";
')
*xx++ = *y++;
') *p_x++ = *p_y++;
Since you can't modify the array addresses (done by x++and y++in your code) and you canmodify the pointer address, I copied over the address of the array into separate pointers and thenincremented them.
既然你不能修改数组地址(通过完成x++并y++在你的代码),你可以修改指针的地址,我复制了数组指针独立的地址,然后增加他们。
If you want, I'm sure you can reduce the notation, but I hope you got the point.
如果你愿意,我相信你可以减少符号,但我希望你明白这一点。
回答by CB Bailey
xand yare arrays, not pointers.
x和y是数组,而不是指针。
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.
它们在大多数表达式上下文中衰减为指针,例如您的增量表达式,但它们衰减为右值,而不是左值,并且您只能将增量运算符应用于左值。
回答by Jerry Coffin
Since you've defined both xand yas arrays, you can't modify them. One possibility would be to use pointers instead:
由于您已将x和都定义y为数组,因此无法修改它们。一种可能性是改用指针:
int q;
int *const p = &q;
p = NULL; // this is not allowed.
Note that I've also fixed your termination condition -- a pointer won't become NULLjust because it's reached the end of a string.
请注意,我还修复了您的终止条件——指针不会NULL仅仅因为它到达字符串的末尾而变成。
回答by user2959760
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。
So you do modify array when you y++!!
所以你在修改数组的时候y++!!
It will produce error: lvalue required as increment operand.
它会产生error: lvalue required as increment operand.
回答by EvanL00
We can not modify a array name, but What about argv++in f(int argv[])?
我们不能修改数组名,但是argv++inf(int argv[])呢?
Quotes from K&R in p99 “an array name is not a varible; construction like a = paand a++are illegal" which says the name of an array is a synonym for the location of the initial element.”
引用自第 99 页的 K&R “数组名称不是变量;构造喜欢a = pa和a++是非法的”,这表示数组的名称是初始元素位置的同义词。”
But why in function parameter func(char *argv[]), we can do argv++despite of argvis a array name.
但是为什么在函数参数中func(char *argv[]),我们可以做argv++尽管 ofargv是一个数组名称。
And in int *a[10], we can't do the a++like argv++.
而在int *a[10],我们不能做的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. type *arrayname_para[]in parameter is a another synonym for type **arrayname_para.
在函数参数中,如char *argv[],与 相同char **argv。 type *arrayname_para[]in 参数是 的另一个同义词type **arrayname_para。
回答by Sujon
Arrays are constant pointers. We can't change them.
数组是常量指针。我们无法改变它们。
##代码##
