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

Mys*_*igs 29 c char variable-assignment lvalue

我在这里 char text[60];

然后我做了一个if:

if(number == 2)
  text = "awesome";
else
  text = "you fail";
Run Code Online (Sandbox Code Playgroud)

它总是说表达式必须是可修改的L值.

MBy*_*ByD 37

你不能改变它的值,text因为它是一个数组,而不是一个指针.

将它声明为char指针(在这种情况下,最好将其声明为const char*):

const char *text;
if(number == 2) 
    text = "awesome"; 
else 
    text = "you fail";
Run Code Online (Sandbox Code Playgroud)

或者使用strcpy:

char text[60];
if(number == 2) 
    strcpy(text, "awesome"); 
else 
    strcpy(text, "you fail");
Run Code Online (Sandbox Code Playgroud)

  • :).左值表示左值(应该是可分配的) (25认同)