What is LValues and RValues in objective c?

☆樱花仙子☆ 提交于 2019-12-25 03:22:59

问题


There are two kinds of expressions in Objective-C

1. RValue

 The term rvalue refers to a data value that is stored at some address in memory

2. LValue

Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment

I didn't get it . can someone explain it to me?


回答1:


RValue is a value that is evaluated but does not have a designated memory address to be stored until assigned to such a memory location. For example :

5 * 2 is an expression evaluated to the number 10. This evaluated expression is still not assigned to a memory address (only a temporary one used for the calculation but you cannot directly refer to it) and will be lost if not stored. And this is the role of the LValue to provide a memory location to store the evaluated expression :

int x;
x = 5 * 2;

Here x refers to a certain memory address and the calculated number (10) can now be stored where x refers to (i.e. in the memory space assigned to x) via the assignment operator. So in the example above x is the LValue and the expression 5 * 2 is the RValue




回答2:


Roughly speaking, lvalue are values that can be assigned to. In C and Objective-C, those are usually variables and pointer dereferences. rvalues are results of expressions that can't be assigned to.

Some examples:

int i = 0;
int j = 1;
int *ptr = &i;

// "i" is a lvalue, "1" is a rvalue
i = 1;
// "*ptr" is a lvalue, "j + 1" is a rvalue
*ptr = j + 1;
// lvalues can be used on both sides of assignment
i = j;

// invalid, since "j + 1" is not a rvalue
(j + 1) = 2

See the Wikipedia article and this article here for more details.



来源:https://stackoverflow.com/questions/25500278/what-is-lvalues-and-rvalues-in-objective-c

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