l-value and r-value(左值和右值)
搞了这么久程序了,对这个一直都没有搞的特别清楚,这种态度真可悲。
以下摘自MSDN。
L-Value and R-Value Expressions
("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.")。
表达式指向可修改位置的,被称为“可修改的左值”。一个可修改的左值不能是一个数组类型,一个不完整的类型,或者一个带有const属性的类型。structure和union可以是可修改的左值,它们不能有任何带有const属性的成员。当变量的值是那个位置存储的值,标识符的名字指定了一个存储地址。
("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.")
如果一个标识符指向一个内存地址,和如果它的类型是arithmetic,structure,union,或者一个指针,那么它就是一个可修改的左值。例如,如果一个ptr是一个指向存储区域的指针,那么*ptr就是一个可修改的左值,指定了这个存储区域。
("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:
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.")
posted on 2011-03-16 11:29 chaiyu2002 阅读(235) 评论(0) 编辑 收藏 举报