【问题标题】:Expression must be a modifiable L-value表达式必须是可修改的 L 值
【发布时间】:2011-08-25 21:49:06
【问题描述】:

我这里有char text[60];

然后我在if

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

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

【问题讨论】:

    标签: c char variable-assignment lvalue


    【解决方案1】:

    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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-10
      • 2019-06-28
      • 2020-07-07
      • 2017-08-27
      • 2021-09-17
      • 2014-12-15
      • 2014-12-20
      • 2015-09-19
      相关资源
      最近更新 更多