-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPointerArithmetic_11.cpp
55 lines (51 loc) · 2.14 KB
/
PointerArithmetic_11.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
55
#include<iostream>
using namespace std;
int main()
{
double *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+8 = 4201171,where 8 is size of double
* **************************/
//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+8 = 4201171
* And also the p's address gets incremented by 8(Size of Double) = 4201163+8=4201171
* **************************/
//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 = 4201171
* As it is post decrement, it will store the address in pointer a = 4201171
* Then it decrements : 4201171-8 = 4201163 , where 8 is size of double
* **************************/
//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-8 = 4201155
* And also the p's address gets decremented by 8(Size of Double) = 4201163-8=4201155
* **************************/
return 0;
}