谁能帮我解释一下lvalue到底是什么意思?

chenqj 2001-08-24 09:43:30
比如:
char p[]="afas";
p++;//error!Lvalue required in function main()

是因为p不能被赋值吗?
...全文
466 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Last_Dodo 2001-08-25
  • 打赏
  • 举报
回复
In other words, lvalue is something you can assign value to. So, clearly, const variables can not be lvalue (that is the reason you get error in your code. p is equivalent to const char*). I don't like the term constant lvalue. constant lvalue is rvalue not lvalue.
chenqj 2001-08-25
  • 打赏
  • 举报
回复
我又看了看书,明白了
我一直把p当成了char *,实际上p是char []
两者之间还是有很大区别的
char *可以作为lvalue,char[]就不行
p作为rvalue时可以implicit转换为char *;
hehe
emailcdh 2001-08-25
  • 打赏
  • 举报
回复
left value,你把p看成一个指针,你说还能不能加?
QXLEE 2001-08-25
  • 打赏
  • 举报
回复
lvalue即left value嘛
这是初学者学指针很容易犯的错误
QXLEE 2001-08-25
  • 打赏
  • 举报
回复
char *p[]="afas"; 注意*
magicblue 2001-08-24
  • 打赏
  • 举报
回复
"Lvalue required in function main()"
your programming is ...?

xwu 2001-08-24
  • 打赏
  • 举报
回复
摘自MSDN:
Expressions in C++ can evaluate to “l-values” or “r-values.” L-values are expressions that evaluate to a type other than void and that designate a variable.

L-values appear on the left side of an assignment statement (hence the “l” in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.

The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.

Some examples of correct and incorrect usages are:

i = 7; // Correct. A variable name, i, is an l-value.
7 = i; // Error. A constant, 7, is an r-value.
j * 4 = 7; // Error. The expression j * 4 yields an r-value.
*p = i; // Correct. A dereferenced pointer is an l-value.
const int ci = 7; // Declare a const variable.
ci = 9; // ci is a nonmodifiable l-value, so the
// assignment causes an error message to
// be generated.
((i < 3) ? i : j) = 7; // Correct. Conditional operator (? :)
// returns an l-value.

Note The examples in this section illustrate correct and incorrect usage when operators are not overloaded. By overloading operators, you can make an expression such as j * 4 an l-value.
WQ 2001-08-24
  • 打赏
  • 举报
回复
p是引用吧
不能改变的

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧