-
Notifications
You must be signed in to change notification settings - Fork 1
/
Session15A.cpp
51 lines (38 loc) · 1.15 KB
/
Session15A.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
#include<iostream>
using namespace std;
class Point{
public:
int x,y;
};
int main(){
const int a = 10;
cout<<"a is: "<<a<<"\n";
cout<<"Address of a is: "<<&a<<"\n";
//a = 11; // Modification not allowed
// =====================
// Pointer to a Constant
const int* ptr = &a;
cout<<"ptr is: "<<ptr<<"\n";
cout<<"Address of ptr is: "<<&ptr<<"\n";
cout<<"Value at ptr is: "<<*ptr<<"\n";
ptr++; // This is allowed - We can change contents of Pointer
//(*ptr)++; // This is an error - We cannot change contents of constant
// =====================
// Constant Pointer
int b = 10;
int* const ptr1 = &b;
//ptr1++; // This is an error - We cannot change contents of constant pointer
(*ptr1)++; // This is allowed - We can change contents of variable to which Pointer points
cout<<"b now is: "<<b<<"\n";
// =====================
// Constant Pointer to a Constant
const int* const ptr2 = &a;
//ptr2++; // This is an error - We cannot change contents of constant pointer
//(*ptr2)++; // This is an error - We cannot change contents of constant
Point p1;
p1.x = 10;
p1.y = 20;
Point* p = &p1;
cout<<"x is: "<<*p.x;
cout<<"y is: "<<*p.y;
}