-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPointerArithmetic_8.cpp
54 lines (51 loc) · 2.2 KB
/
PointerArithmetic_8.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
53
54
#include <iostream>
using namespace std;
int main()
{
int *a, *p;
// Post Increment
cout << "Address Value of p in integer before increment(post): " << (int)p << endl;
a = p++;
cout << " Value of a in integer : " << (int)a << endl;
cout << "Address Value of p in integer after increment(post): " << (int)p << endl;
// OPERATION***//
/*****************************
* p address value = 4201163
* As it is post increment, it will store the address in pointer a = 4201163
* Then it increments : 4201163+4 = 4201167 , where 4 is size of Integer
* **************************/
// Pre Increment
a = ++p;
cout << " Value of a in integer : " << (int)a << endl;
cout << "Address Value of p in integer after increment(pre): " << (int)p << endl;
// OPERATION***//
/*****************************
* p address value = 4201163
* As it is pre increment, it will store the incremented value in pointer a
* i.e.: 4201163+4 = 4201167
* And also the p's address gets incremented by 4(Size of INT) = 4201163+4=4201167
* **************************/
// Post Decrement
cout << "Address Value of p in integer before decrement(post): " << (int)p << endl;
a = p--;
cout << " Value of a in integer : " << (int)a << endl;
cout << "Address Value of p in integer after decrement(post): " << (int)p << endl;
// OPERATION***//
/*****************************
* p address value = 4201167
* As it is post decrement, it will store the address in pointer a = 4201167
* Then it decrements : 4201167-4 = 4201163 , where 4 is size of Integer
* **************************/
// Pre Decrement
a = --p;
cout << " Value of a in integer : " << (int)a << endl;
cout << "Address Value of p in integer after decrement(pre): " << (int)p << endl;
// OPERATION***//
/*****************************
* p address value = 4201163
* As it is pre decrement, it will store the decremented value in pointer a
* i.e.: 4201163-4 = 4201159
* And also the p's address gets decremented by 4(Size of INT) = 4201163-4=4201159
* **************************/
return 0;
}