L值VS在C R值 [英] L value vs R value in C

查看:134
本文介绍了L值VS在C R值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回答从本教材教科书问题。

我学习关于C指针和所遇到的L-值和r值。从我的理解:

  L-值是在执行后所定义的值(+ X)
r值是在执行(X ++)后,可以进行不限定值

这是正确的?

我想回答(我尝试)的问题:

  A),它下面的C前pressions是L-值?
   1,X + 2不是L值
   2.&放大器; X是L值
   3. *放大器; X是L值
   4.&功放; X + 2不是L值
   5. *(&安培; X + 2)是L值
   6.&安培; * Y是一个L值b)是否有可能为一个C前pression是一个L值,但不是一个R值?说明
   我读过一个L值可以是一个R值而不是相反。我不能想到的东西作为一个L值而不是R值的例子。C)是及(&安培; Z c)中曾经是否合法?说明
   假设因为内存地址没有自己的内存地址,这是不合法的?

我是关闭?谢谢


解决方案

  

L-值是在执行后所定义的值( ++ X
  R值是在执行后未定义的值( X ++


没有,这是不正确的。

词左值和右值(这是C标准如何拼写他们)有着悠久的历史。术语来自L为左和R为右,指的是一个分配的左,右两侧。

在某些情况下,一个前pression可能是的评价其左值评价其右值的。给出的术语的这些定义中,右值,也就是你通常认为的一个前pression的价值;评估 2 + 2 收益率 4 。评估其左值的前pression意味着确定哪些对象是指。例如,给出:

  INT X;
X = 2 + 2;

转让的右侧, 2 + 2 其右值会被评估,产生 4 ,和的左侧将其左值,这意味着确定其所引用的对象进行评估。 (除权pression的右值是的的评估; pviously存储在 x计算的值$ P $ ,如果有的话,将不使用。)

C标准定义它们是不同的。在C语言中,一个左值的不是一个值;它是一种前pression的。具体来说,引用了2011 ISO C标准,第6.3.2.1节:


  

这是左值的是前pression(比其他无效的对象类型)
  可能指定的对象;如果一个左值没有指定的
  当它被评价对象的行为是不确定的。


(潜在的加字涵盖样病例 * PTR ,其中 PTR 是一个指向对象;如果 PTR == NULL 然后 * PTR 不的目前的指定对象,但它仍然是一个左值。你总是可以在编译期确定一个给定的前pression是否是左值或没有。C标准的早期版本已经有缺陷的的左值的定义。)

所以基本上一个的左值的在C是一个前pression,指定的对象。你可以把它理解为可以转让的左侧出现一个前pression,尽管这不完全准确;例如,常量的名称对象不能在赋值的LHS,但它仍然是一个左值。 (正如你所看到的,钉下了precise和一致的定义的左值的可能会非常棘手。)

无论是 X ++ 也不 ++ X 是在C的左值。

C标准不使用术语的右值的超越单一的脚注提的是:


  

什么有时被称为右值是在本国际标准
  描述为一个前pression的价值。


所以,作为C定义的术语,一个的左值的是一种前pression的(东西存在于C源$ C ​​$ C),而是一个的右值是评估一个前pression(一些程序执行过程中存在的)的结果。

(C ++有不同的规则,和其他几个班的左值状的东西,包括的 glvalues​​ 的等等。我不会进入这里。)

I am answering a textbook question from this textbook.

I am learning about pointers in C and have come across l-values and r-values. From my understanding:

l-values are values that are defined after they are executed (++x)
r-values are values that are not defined after they are executed (x++)

It that correct?

The question I wanted to answer (with my attempts):

a) Which of the following C expressions are L-Values?
   1.  x + 2 Not a L value
   2.  &x    Is a L value
   3.  *&x   Is a L value
   4.  &x + 2  Not a L value
   5.  *(&x + 2) Is a L value
   6.  &*y  Is a L value

b) Is it possible for a C expression to be a L-value but NOT a R-value? Explain
   I've read that a L value can be a R value but not vice versa. I can't think of an example of something being an L value but not a R value.

c) Is &(&z) ever legal in C? Explain
   Assuming this is not legal since a memory address doesn't have its own memory address?

Am I close? Thanks

解决方案

l-values are values that are defined after they are executed (++x) r-values are values that are not defined after they are executed (x++)

No, that's not correct.

The words "lvalue" and "rvalue" (that's how the C standard spells them) have a long history. The terms come from 'l' for "left" and 'r' for "right", referring to the left and right sides of an assignment.

In some contexts, an expression may be either evaluated for its lvalue or evaluated for its rvalue. Given those definitions of the terms, an "rvalue" is what you'd normally think of as the value of an expression; evaluating 2+2 yields 4. Evaluating an expression for its lvalue meant determining what object it refers to. For example, given:

int x;
x = 2 + 2;

the right side of the assignment, 2 + 2 would be evaluated for its rvalue, yielding 4, and the left side would be evaluated for its lvalue, which means determining the object to which it refers. (The rvalue of the expression is not evaluated; the value previously stored in x, if any, is not used.)

The C standard defines them differently. In C, an lvalue is not a value; it's a kind of expression. Specifically, quoting the 2011 ISO C standard, section 6.3.2.1:

An lvalue is an expression (with an object type other than void) that potentially designates an object; if an lvalue does not designate an object when it is evaluated, the behavior is undefined.

(The word "potentially" was added to cover cases like *ptr, where ptr is a pointer object; if ptr == NULL then *ptr doesn't currently designate an object, but it's still an lvalue. You can always determine at compile time whether a given expression is an lvalue or not. Earlier editions of the C standard has flawed definitions for lvalue.)

So basically an lvalue in C is an expression that designates an object. You can think of it as an expression that can appear on the left side of an assignment, though that's not entirely accurate; for example, the name of a const object can't be on the LHS of an assignment, but it's still an lvalue. (As you can see, nailing down a precise and consistent definition for lvalue can be tricky.)

Neither x++ nor ++x is an lvalue in C.

The C standard doesn't use the term rvalue beyond mentioning it in a single footnote:

What is sometimes called "rvalue" is in this International Standard described as the "value of an expression".

So, as C defines the terms, an lvalue is a kind of expression (something that exists in C source code), but an rvalue is the result of evaluating an expression (something that exists during program execution).

(C++ has different rules, and several other classes of lvalue-like things, including glvalues and so forth. I won't get into that here.)

这篇关于L值VS在C R值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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