What is LValues and RValues in objective c?

2019-09-01 07:12发布

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?

2条回答
你好瞎i
2楼-- · 2019-09-01 07:44

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

查看更多
成全新的幸福
3楼-- · 2019-09-01 07:52

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.

查看更多
登录 后发表回答