表达式必须是可修改的 L 值 [英] Expression must be a modifiable L-value

查看:16
本文介绍了表达式必须是可修改的 L 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有char text[60];

然后我在 if 中做:

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

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

推荐答案

lvalue 表示左值";- 它应该是可分配的.你不能改变 text 的值,因为它是一个数组,而不是一个指针.

lvalue means "left value" -- it should be assignable. You cannot change the value of text since it is an array, not a pointer.

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

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

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

或者使用strcpy:

Or use strcpy:

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

这篇关于表达式必须是可修改的 L 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆