Pointers

advertisement
Pointers
Overview
 What
 How
 Use
are Pointers?
to use Pointers?
of Pointers
Pointer:
A
variable that can only store the address
of a memory location (usually the address
of other variable).
x
500
a
300
300
pointer
pointee
Syntax:

type * identifier;
i.e. int *p;
p
258
G
pointer
 int
a=50;
p
500
 int
p
* p;
= &a;
p points to a
a
300
300
50
pointer
pointee
* and & operators
 Dereferencing
*p
value at where pointer p is pointing
 Address
of
&a
address of variable a
Example:
void main(){
int *p;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
Example:
void main(){
=>
int *p;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
pointer
Example:
void main(){
int *p;
=>
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
50
pointer
pointee
Example:
void main(){
int *p;
int a=50;
=>
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
Example:
void main(){
int *p;
int a=50;
p = &a;
=>
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
// 50
Example:
void main(){
int *p;
int a=50;
p = &a;
cout<<a<<endl;
=>
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
// 50
// 300
Example:
void main(){
int *p;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
=>
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
// 50
// 300
// 300
Example:
void main(){
int *p;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
=>
cout<<*p<<endl;
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
// 50
// 300
// 300
// 50
Example:
void main(){
int *p;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
=>
cout<<&p<<endl;
}
p
500
a
300
300
50
pointer
pointee
// 50
// 300
// 300
// 50
// 500
Example 2:
void main(){
int *p;
int a=50;
cout<<*p<<endl;
}
p
500
a
300
Crash!!!
50
pointer
pointee
What would be the output?
Runtime error!!!
Dangling Pointer
int *p;
p
500
G
pointer
Dangling pointer
Note: Dereferencing a dangling pointer is a
serious runtime error.
Problem:
We can’t differentiate that whether a
pointer is dangling or has a valid address.
What’s the solution?
NULL Pointer
int *p;
p = NULL;
// points to
// nothing
p
500
pointer
Tip: Always initialize a pointer with NULL if
you don’t have a valid address. This can
save a lot of your time on debugging.
Example:
void main(){
int *p = NULL;
int a=50;
p = &a;
cout<<a<<endl;
cout<<&a<<endl;
cout<<p<<endl;
cout<<*p<<endl;
cout<<&p<<endl;
}
Pointer Assignment
 An
assignment operation b/w two
pointers makes them points to the same
pointee.
 int
 int
p
a=50;
* p;
= &a;
 int
p2 = p;
p
500
p points to a
a
300
300
50
pointer
pointee
p2
200
300
pointer
Note: Pointer assignment only copies the address and not the
memory they are pointing to.
Shallow Copy
p
500
p points to a
a
300
300
50
pointer
pointee
p2
200
300
pointer
Problem with Shallow Copy
p
500
p points to a
a
300
300
50
pointer
pointee
p2
200
300
pointer
What if p
delete a?
What is the solution to
the problem?
Deep Copy
p
500
p points to a
a
300
300
50
pointer
pointee
p2
200
a2
100
100
50
pointer
pointee
Pointer type and Arithmetic
 What
is type of pointer?
int * p;
A pointer has not type!!!
It’s the type of variable the pointer p will
point to.
 Why
we need to specify the type of
variable a pointer points to?
1.
int a=10;
int *p = &a;
*p = 4; // how many bytes of copy?
It would be 1 byte in case a was a char.
2. Pointer & Arrays
40 42 44
int arr[ ]={5,6,7,8,4,3,5};
5 6 7
// size of arr?
p
int *p = arr;
500
// why & isn’t use before arr?
40
cout<<arr<<endl; // 40
pointer
cout<<p<<endl;
// 40
p=p+3;
46 48 50
8
4
3
52
5
2. Pointer & Arrays
40 42 44
int arr[ ]={5,6,7,8,4,3,5};
5 6 7
// size of arr?
p
int *p = arr;
500
// why & isn’t use before arr?
40
cout<<arr<<endl; // 40
pointer
cout<<p<<endl;
// 40
p=p+3;
cout<<p<<endl;
// 46
cout<<*p<<endl; // 8
46 48 50
8
4
3
52
5
Example 3:
void main( ){
=> int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
}
cout<< arr<<endl;
cout<<*arr<<endl;
cout<<*(arr+4)<<endl;
cout<<p<<endl;
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
40 42 44
46 48 50
5
8
6
7
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
=> int *p = arr;
}
cout<< arr<<endl;
cout<<*arr<<endl;
cout<<*(arr+4)<<endl;
cout<<p<<endl;
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
=> cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
cout<<*(arr+4)<<endl;
cout<<p<<endl;
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
=> cout<<*arr<<endl; // 5
cout<<*(arr+4)<<endl;
cout<<p<<endl;
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
=> cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
=> cout<<p<<endl;
//0x40
cout<<&p<<endl;
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
//0x40
=> cout<<&p<<endl;
//0x500
cout<<*p<<endl;
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
//0x40
cout<<&p<<endl;
//0x500
=> cout<<*p<<endl;
// 5
cout<<p[3]<<endl;
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
//0x40
cout<<&p<<endl;
//0x500
cout<<*p<<endl;
// 5
=> cout<<p[3]<<endl; // 8
cout<<p++<<endl;
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
//0x40
cout<<&p<<endl;
//0x500
cout<<*p<<endl;
// 5
cout<<p[3]<<endl; // 8
=> cout<<p++<<endl; // 0x40
cout<<++*p<<endl;
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Example 3:
void main( ){
int arr[ ]={5,6,7,8,4,3,5};
int *p = arr;
cout<< arr<<endl;
// 0x40
cout<<*arr<<endl;
// 5
cout<<*(arr+4)<<endl;
// 4
cout<<p<<endl;
//0x40
cout<<&p<<endl;
//0x500
cout<<*p<<endl;
// 5
cout<<p[3]<<endl; // 8
cout<<p++<<endl; // 0x40
=> cout<<++*p<<endl; // 7
}
40 42 44
46 48 50
5
8
6
7
p
500
40
pointer
4
3
52
5
Arrays of Pointer
data_type *arr[size];
i.e.
int *a[7];
char *c[7];
40 42 44
46 48 50
*
*
*
*
*
*
80 82 84
86 88 90
*
*
*
*
*
*
52
*
92
*
Uses:
 Sharing

& Cost saving
Pass-by-ref
 Dynamic
 Making

Memory Allocation
Complex Data Structures
Linked list, Tree etc.
1. Sharing & cost saving
void main(){
int a=5,b=8;
swap(&a,&b);
}
void swap(int *a,int *b){
int t = *a;
*a = *b;
*b = t;
}
2. DMA
void main(){
int size=0;
int *p=NULL;
cout<<“Enter No. of Students”<<endl;
cin>>size;
p = (int *)malloc(size*sizeof(int));
for(int i=0;i<size;i++){
cout<<“Enter St “<<i<<“ Marks”<<endl;
cin>>p[i]; //= cin>>*p; p++;
}
}
3. Data Structures
Data
Pointer
Data
Pointer
Data
Pointer
Data
Pointer
References:
 Pointer
And Memory
(http://cslibrary.stanford.edu/102/)
 Pointer Basics
(http://cslibrary.stanford.edu/106/)
 http://www.cprogramming.com/tutorial/
c/lesson6.html
 http://www.cs.cf.ac.uk/Dave/C/node10.h
tml
Download