-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_arithmetic.cpp
More file actions
60 lines (48 loc) · 1.93 KB
/
Copy pathpointer_arithmetic.cpp
File metadata and controls
60 lines (48 loc) · 1.93 KB
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
53
54
55
56
57
58
59
60
/* How will we can use the operators that arithmetic process and which operators can we use with pointers?t
0: increase the pointer --> ++
1: decrease the pointer --> --
2: adding a integer to the pointer --> +=
3: subtraction a integer from the pointer --> -=
4: a pointer substraction from amother pointer --> ptr1 - ptr2
ex:
adresses--> [3000] [3004] [3008] ....
vptr -----> v[ 0 ] // v[ 1 ] // v[ 2 ] ....
*vptr ----> 0 // 1 // 2 ....
vptr += 4 -> 3016 --> (3000 + 4 * 4(byte for int))
*/
#include <iostream>
using namespace std;
int main(){
int v[ 10 ] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int *v_ptr = &v[ 0 ]; // same mean with *v_ptr1 = v
cout << " v_ptr : " << v_ptr << " --> " << *v_ptr << endl;
// 0: increase the pointer --> ++
v_ptr++;
cout << " v_ptr++ : " << v_ptr << " --> ";
cout << *v_ptr << endl;
// 1: decrease the pointer --> --
v_ptr--;
cout << " v_ptr-- : " << v_ptr << " --> ";
cout << *v_ptr << endl;
// 2: adding a integer to the pointer --> +=
v_ptr += 4;
cout << " v_ptr += 4 : " << v_ptr << " --> ";
cout << *v_ptr << endl;
// 3: subtraction a integer from the pointer --> -=
v_ptr -= 2;
cout << " v_ptr -= 2 : " << v_ptr << " --> ";
cout << *v_ptr << endl;
// 4: a pointer substraction from amother pointer --> ptr1 - ptr2
int *v2ptr = v_ptr + 5;
cout << " v2ptr : " << v2ptr << " --> ";
cout << *v2ptr << endl;
int *vptr = v_ptr;
cout << " vptr : " << vptr << " --> ";
cout << *vptr << endl;
// there is difference of this adresses total 20 bytes memory in RAM.
// each int variable allocating 4 bytes memory in RAM.
int x= v2ptr - vptr; // so this eq is return the 20/4=5 bytes..
cout << " v2ptr - vptr " << v2ptr << " - " << vptr << " --> ";
cout << x << endl;
return 0;
}