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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6008733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:39:52  来源:igfitidea点击:

Expression must be a modifiable L-value

ccharvariable-assignmentlvalue

提问by Mysterigs

I have here char text[60];

我这里有 char text[60];

Then I do in an if:

然后我在一个if

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

and it always said expression must be a modifiable L-value.

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

回答by MByD

You cannot change the value of textsince it is an array, not a pointer.

您不能更改的值,text因为它是一个数组,而不是一个指针。

Either declare it as char pointer (in this case it's better to declare it as const char*):

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

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

Or use strcpy:

或者使用 strcpy:

char text[60];
if(number == 2) 
    strcpy(text, "awesome"); 
else 
    strcpy(text, "you fail");