表达式必须是可修改的L值

时间:2011-05-15 13:41:43

标签: c char variable-assignment lvalue

我在这里char text[60];

然后我在if

if(number == 2)
  text = "awesome";
else
  text = "you fail";

并且总是说表达式必须是可修改的L值。

1 个答案:

答案 0 :(得分:36)

您无法更改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");
相关问题