我这里有一个char text[60];
然后我在一个if
语句中执行以下操作:
if(number == 2)
text = "awesome";
else
text = "you fail";
它总是报错说表达式必须是可修改的左值。
我这里有一个char text[60];
然后我在一个if
语句中执行以下操作:
if(number == 2)
text = "awesome";
else
text = "you fail";
它总是报错说表达式必须是可修改的左值。
lvalue
意味着“左值” -- 它应该是可赋值的。由于它是一个数组而不是指针,因此您无法更改 text
的值。
要么将其声明为 char 指针(在这种情况下最好将其声明为 const char*
):
const char *text;
if(number == 2)
text = "awesome";
else
text = "you fail";
或者使用 strcpy:
char text[60];
if(number == 2)
strcpy(text, "awesome");
else
strcpy(text, "you fail");