ant2009 ant2009 - 1 month ago 20
C Question

lvalue required as increment operand

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.

Answer

x++ is the short form of x = x + 1. However, x here is an array and you cannot modify the address of an array. So is the case with your variable y too. What you can do is defined two pointers pointing to the two arrays and increment those pointers or do the following.

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