左值 右值
来源:互联网 发布:阿青和独孤求败知乎 编辑:程序博客网 时间:2024/04/28 09:52
L-Value and R-Value Expressions
Expressions that refer to memory locations are called “l-value” expressions. An l-value represents a storage region’s “locator” value, or a “left” value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.
Expressions referring to modifiable locations are called “modifiable l-values.” A modifiable l-value cannot have an array type, an incomplete type, or a type with the const attribute. For structures and unions to be modifiable l-values, they must not have any members with the const attribute. The name of the identifier denotes a storage location, while the value of the variable is the value stored at that location.
An identifier is a modifiable l-value if it refers to a memory location and if its type is arithmetic, structure, union, or pointer. For example, if ptr is a pointer to a storage region, then *ptr is a modifiable l-value that designates the storage region to which ptr points.
Any of the following C expressions can be l-value expressions:
An identifier of integral, floating, pointer, structure, or union type
A subscript ([ ]) expression that does not evaluate to an array
A member-selection expression (–> or .)
A unary-indirection (*) expression that does not refer to an array
An l-value expression in parentheses
A const object (a nonmodifiable l-value)
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.
Microsoft Specific —>
Microsoft C includes an extension to the ANSI C standard that allows casts of l-values to be used as l-values, as long as the size of the object is not lengthened through the cast. (See Type-Cast Conversions for more information.) The following example illustrates this feature:
char *p ;
short i;
long l;
(long *) p = &l ; /* Legal cast */
(long) i = l ; /* Illegal cast */
The default for Microsoft C is that the Microsoft extensions are enabled. Use the /Za compiler option to disable these extensions.
END Microsoft Specific
3 . 前增量和后增量的区别
int a = 0;
(++a) = 10;// 可以
(a++) = 10;//不可以
左值和右值这两个概念我想大家都耳熟能详了,但它们究竟是什么以及如何理解它运用它,一开始大家都会感觉比较困难。所以我想写这篇总结性的文章来帮助大家理解它们。希望对大家有帮助。
首先我们需要理解左值和右值的定义:
左值指的是如果一个表达式可以引用到某一个对象,并且这个对象是一块内存空间且可以被检查和存储,那么这个表达式就可以做为一个左值。
右值指的是引用了一个存储在某个内存地址里的数据。
从上面的两个定义可以看出,左值其实要引用一个对象,而一个对象在我们的程序中又肯定有一个名字或者可以通过一个名字访问到,所以左值又可以归纳为:左值表示程序中必须有一个特定的名字引用到这个值。而右值引用的是地址里的内容,所以相反右值又可以归纳为:右值表示程序中没有一个特定的名字引用到这个值除了用地址。
好这些都是从定义上理解左值右值,那么我们再用这些定义作为我们的理论基础来总结一下哪些是左值,哪些是右值:
左值:
Expression
Lvalue
x = 42
x
*ptr = newvalue
*ptr
a++
a++
b[0] = 100
b[0]
const int m = 10
m
int& f()
The function call to f()
右值:
Expression
Rvalue
100
100
a * b
The expression of a * b
int f()
The function call to f() that does not return reference
以上这些内容都可以用定义来解释为什么这些为左值,而那些为右值。但我要特殊解释一下为什么函数的调用只能作为右值除了这个函数返回的是引用。其实这个也非常好解释,因为如果一个函数返回的值是 内建类型,那么这个返回值是没有办法通过一个名字或者表达式引用到的,同理如果一个函数返回的是一个对象,那么这个对象是一个临时的,也不可能用一个名字访问到。所以函数的调用通常只能作为右值,但如果一个函数返回引用,那么它的返回值就有意义了,因为它是另一个名字的别名,有名字了,所以它就变成了左值。
注意:左值能转化为右值,但反之不行。
好了,讲了这么多我觉得已经足够,但还要多讲一点,这点就是哪些操作符必需左值.
Operator
Requirement
& (unary)
Operand must be an lvalue.
++ --
Operand must be an lvalue. This applies to both prefix and postfix forms.
= += -= *= %= <<= >>= &= ^= |=
Left operand must be an lvalue.
- 左值 右值
- 左值 右值
- 左值 右值
- 左值 右值
- 左值 右值
- 左值 右值
- 左值/右值
- 左值、右值、左值引用、右值引用
- 左值、右值、左值引用、右值引用
- 左值 右值 左值引用 右值引用
- 左值与右值
- 左值与右值
- 左值与右值
- 左值和右值
- 左值和右值
- 左值和右值
- 左值与右值
- 左值和右值
- 标准C++中的STL容器类简介
- UGG Women's Classic Short 5825 中筒雪地靴批发+零售
- Make a Cooliris Holiday Card
- 博客乔迁
- vc6能用的SDK和库列表
- 左值 右值
- CLIPS-读写关闭文件
- c/c++ 左值 右值讨论
- vc6 sdk
- sola ris 简单命令
- UNIX Shell编程 学习笔记(二)
- 索尼爱立信网络软件商店“PlayNow” .桂赚军荐
- 在jsp页面间传递参数
- 数据库查询优化原则,欢迎指正补充