-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPointerArithmetic_13.cpp
70 lines (61 loc) · 2.88 KB
/
PointerArithmetic_13.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
int main()
{
char *a, *p;
//Post Increment
cout<<"Address Value of p in integer before increment(post): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character before increment(post): "<<p<<endl;
a = p++;
cout<<" Value of a in integer : "<<(int)a<<endl;
cout<<" Value of a in ASCII character : "<<a<<endl;
cout<<"Address Value of p in integer after increment(post): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character after increment(post): "<<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+1 = 4201164, where 1 is size of char
* **************************/
//Pre Increment
a = ++p;
cout<<" Value of a in integer : "<<(int)a<<endl;
cout<<" Value of a in ASCII character : "<<a<<endl;
cout<<"Address Value of p in integer after increment(pre): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character after increment(pre): "<<p<<endl;
//OPERATION***//
/*****************************
* p address value = 4201163
* As it is pre increment, it will store the incremented value in pointer a
* i.e.: 4201163+1 = 4201164
* And also the p's address gets incremented by 1(Size of Char) = 4201163+1=4201164
* **************************/
//Post Decrement
cout<<"Address Value of p in integer before decrement(post): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character before decrement(post): "<<p<<endl;
a = p--;
cout<<" Value of a in integer : "<<(int)a<<endl;
cout<<" Value of a in ASCII character : "<<a<<endl;
cout<<"Address Value of p in integer after decrement(post): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character after decrement(post): "<<p<<endl;
//OPERATION***//
/*****************************
* p address value = 4201164
* As it is post decrement, it will store the address in pointer a = 4201164
* Then it decrements : 4201164-1 = 4201163 , where 1 is size of CHAR
* **************************/
//Pre Decrement
a = --p;
cout<<" Value of a in integer : "<<(int)a<<endl;
cout<<" Value of a in ASCII character : "<<a<<endl;
cout<<"Address Value of p in integer after decrement(pre): "<<(int)p<<endl;
cout<<"Address Value of p in ASCII character after decrement(pre): "<<p<<endl;
//OPERATION***//
/*****************************
* p address value = 4201163
* As it is pre decrement, it will store the decremented value in pointer a
* i.e.: 4201163-1 = 4201162
* And also the p's address gets decremented by 1(Size of CHAR)→ 4201162-1 = 4201161
* **************************/
return 0;
}