The code below gives me the following error:
incompatible types when assigning to type’char[10]’ from type char*
lvalue required as decrement operand.
What might cause this?
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1="1234";
char str2[10];
str2 = str2 + strlen(str1)-1; //the str pointer is at 3rd position
char *p = str2+1; //since it has to be a valid string, i assigned pointer p to give the null value at the end of the string.
*p = '\0';
while(*(str2--) = *(str1++)) //moving the pointer of str down and pointer of str1 up and copy char from str1 to str2
printf("%s", str2);
return 0;
}
str2 is an array, not a pointer, so on the left hand side
str2 = ...is not a meaningful C expression.Either str2 must be a pointer, or use an extra variable, like p, to get the expression
Similarly
(str2--)is not meaningful. Use an extra character pointer to str2, and change that.Think of an array name as a constant pointer. It can’t be changed, but can be used on the right hand side of an
=in an expression.