char []与自增操作符++
文章目录
char x[10];
char y[] = "Hello";
while(y != NULL)
*x++ = *y++;
这段代码是错误的(lvalue required as increment operand),因为x++即x = x+1. 所以x必须是个左值lvalue.然而x是数组(具体的值是数组首元素的地址),我们不能修改数组的地址。同样,y也是。
可以这样做:定义指针指向两个数组,对指针做自增
char x[10];
char y[] = "Hello";
int i=0;
while(y[i] != 0)
{
*(x+i) = *(y+i);//指针
i++;
}
x[i] = 0;

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论