-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathConstPointers.cpp
More file actions
31 lines (19 loc) · 776 Bytes
/
ConstPointers.cpp
File metadata and controls
31 lines (19 loc) · 776 Bytes
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
#include<iostream>
//const pointers
using namespace std;
int main()
{
int x=10,y=20;
int* const ptr = &x;//a read-only pointer which refers to x, now ptr value cannot be changed i.e it cannot refer to any other variable
*ptr=20;
// x=20; //can be done
// ptr=&y;// COMPILE TIME ERROR-cannot change value of the pointer as its is read-only/constant
cout<<x<<endl;// output=10
//cout<<++*ptr produces error as *ptr is read-only i.e const and its value cannot be changed
cout<<&x<<endl; // output-memory address of x
cout<<ptr<<endl; // output-memory address of ptr
cout<<++x<<endl;//first increment then print-x becomes = 11
cout<<x++<<endl;//output-11 as first it prints then increments value of x to 12
cout<<x;//now prints 12
return 0 ;
}