-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep22.cpp
More file actions
43 lines (38 loc) · 1.06 KB
/
practicep22.cpp
File metadata and controls
43 lines (38 loc) · 1.06 KB
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
#include <iostream>
using namespace std;
//pointers
int main(){
int a=4;
int *p=&a; //points to the value stored in the address of a
//int *p
//p=&a;
cout<<a<<endl;
cout<<&a<<endl; //prints the address.
cout<<p<<endl; //prints the address same as &p.
cout<<*p<<endl;
*p=10;
a=56;
cout<<a<<endl; //prints 56.
*p=6;
cout<<a<<endl;
cout<<*p<<endl; //prints 10.
//Dangling Pointers
int *ptr;
{
int v=23;
ptr=&v;
cout<<"Value is(inside block): "<<*ptr<<endl;
cout<<"Address is(inside block): "<<ptr<<endl;
}
//still we are able to access
cout<<"Value is(outside block): "<<*ptr<<endl;
cout<<"Address is(outside block): "<<ptr<<endl;
//to overcome this
//we make ptr as NULL
ptr=NULL;
//*ptr=NULL will only make the value inside ptr as NULL not the pointer itself.
//the below code wont print as ptr is NULL(no pointer)
cout<<"value after null(outside block): "<<*ptr<<endl;
cout<<"Address after null(outside block): "<<ptr<<endl;
return 0;
}