Are literal strings and function return values lvalues or rvalues?

蓝咒 提交于 2019-11-26 16:08:27
  1. string literals are lvalues, but you can't change them
  2. rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue

The C standard recognizes the original terms stood for left and right as in L = R; however, it says to think of lvalue as locator value, which roughly means you can get the address of an object and therefore that object has a location. (See 6.3.2.1 in C99.)

By the same token, the standard has abandoned the term rvalue, and just uses "the value of an expression", which is practically everything, including literals such as ints, chars, floats, etc. Additionally, anything you can do with an rvalue can be done with an lvalue too, so you can think of all lvalues as being rvalues.

There are two kinds of expressions in C: 1. lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. 2. rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and cannot appear on the left-hand side. Following is a valid statement: int g = 20; But following is not a valid statement and would generate compile-time error: 10=20;

there's a definition for C++ from Microsoft. By this definition, a literal string, say "hello world", is lvalue, because it's const and not temporary. Actually it persists across your application's lifetime.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!