-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvalues_rvalues.cpp
52 lines (38 loc) · 1.17 KB
/
lvalues_rvalues.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
int square(int);
int main()
{
// L-value:
// 1. We can take the address of the expression.
// 2. This object lasts longer.
// R-value:
// 1. We cannot take the address of the expression.
// 2. This object is temporary.
int a = 15; // here a - Lvalue and 15 - rValue
int b = 25; // here b - Lvalue and 25 - rValue
// we can get the memory of a and b
// using &a and &b, but we cannot get the
// address of 15 and 25.
// Also, a and b last longer, whereas 15 and
// 25 are temporary.
// Eg. 2
int sum = a + b;
// here sum -> lvalue and expression, a + b -> rvalue
// we can get the memory address of sum using
// &sum. But we cannot get the memory address
// of &(a+b) -> this is temporary and throws error;
int *pointer = ∑ // no error
// Error: expression must be an lvalue or a function designator
// int *pointer = &(a + b) // error
// this throw error. We cannot take the address of
// function - square()
// int *pointer2 = &square(a);
// this is valid.
int *pointer3 = &a;
return 0;
}
int square(int x)
{
return x * x;
}