pointers. overview what are pointers? how to use pointers? use of pointers

48
Pointers

Upload: jeremy-chapman

Post on 30-Dec-2015

257 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Pointers

Page 2: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Overview What are Pointers?

How to use Pointers?

Use of Pointers

Page 3: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Pointer: A variable that can only store the

address of a memory location (usually the address of other variable).

300

x500

a300

pointer pointee

Page 4: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Syntax: type * identifier;

i.e. int *p;

G

p258

pointer

Page 5: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

int a=50;

int * p;

p = &a;

p500

pointer

50

a300

pointee

p points to a

300

Page 6: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

* and & operators Dereferencing

*pvalue at where pointer p is

pointing

Address of&aaddress of variable a

Page 7: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;

}

Page 8: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;

}

p500

pointer

Page 9: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;

}

p500

pointer

50

a300

pointee

Page 10: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;

}

300

p500

pointer

50

a300

pointee

Page 11: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example:

void main(){int *p;int a=50;p = &a;

=> cout<<a<<endl; // 50cout<<&a<<endl;cout<<p<<endl;cout<<*p<<endl;cout<<&p<<endl;

}

300

p500

pointer

50

a300

pointee

Page 12: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example:

void main(){int *p;int a=50;p = &a;cout<<a<<endl; // 50

=> cout<<&a<<endl; // 300cout<<p<<endl;cout<<*p<<endl;cout<<&p<<endl;

}

300

p500

pointer

50

a300

pointee

Page 13: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example:

void main(){int *p;int a=50;p = &a;cout<<a<<endl; // 50cout<<&a<<endl; // 300

=> cout<<p<<endl; // 300cout<<*p<<endl;cout<<&p<<endl;

}

300

p500

pointer

50

a300

pointee

Page 14: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example:

void main(){int *p;int a=50;p = &a;cout<<a<<endl; // 50cout<<&a<<endl; // 300cout<<p<<endl; // 300

=> cout<<*p<<endl; // 50cout<<&p<<endl;

}

300

p500

pointer

50

a300

pointee

Page 15: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example:

void main(){int *p;int a=50;p = &a;cout<<a<<endl; // 50cout<<&a<<endl; // 300cout<<p<<endl; // 300cout<<*p<<endl; // 50

=> cout<<&p<<endl; // 500}

300

p500

pointer

50

a300

pointee

Page 16: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Example 2:

void main(){int *p;int a=50;cout<<*p<<endl;

}

What would be the output?Runtime error!!!

p500

pointer

50

a300

pointee

Crash!!!

Page 17: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Dangling Pointerint *p;

Note: Dereferencing a dangling pointer is a serious runtime error.

G

p500

pointerDangling pointer

Page 18: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Problem:We can’t differentiate that whether a pointer is dangling or has a valid address.

What’s the solution?

Page 19: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

NULL Pointerint *p;p = NULL; // points to

// nothing

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.

p500

pointer

Page 20: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;

}

Page 21: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Pointer Assignment An assignment operation b/w two

pointers makes them points to the same pointee.

Page 22: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

int a=50;

int * p;

p = &a;

int p2 = p;

p500

pointer

50

a300

pointee

p points to a

300

p2200

pointer Sharing

300

Note: Pointer assignment only copies the address and not the memory they are pointing to.

Page 23: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Shallow Copy

300

p500

pointer

50

a300

pointee

p points to a

300

p2200

pointer Sharing

Page 24: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Problem with Shallow Copy

300

p500

pointer

50

a300

pointee

p points to a

300

p2200

pointer Wild Pointer

What if p delete a?

Page 25: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

What is the solution to the problem?

Page 26: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Deep Copy

300

p500

pointer

50

a300

pointee

p points to a

100

p2200

pointer

50

a2100

pointee

Page 27: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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.

Page 28: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Why we need to specify the type of variable a pointer points to?

Page 29: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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.

Page 30: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

2. Pointer & Arraysint arr[ ]={5,6,7,8,4,3,5};// size of arr?int *p = arr;// why & isn’t use before arr?cout<<arr<<endl; // 40cout<<p<<endl; // 40p=p+3;

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 31: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

2. Pointer & Arraysint arr[ ]={5,6,7,8,4,3,5};// size of arr?int *p = arr;// why & isn’t use before arr?cout<<arr<<endl; // 40cout<<p<<endl; // 40p=p+3;cout<<p<<endl; // 46cout<<*p<<endl; // 8

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 32: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

Page 33: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 34: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 35: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 36: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 37: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 38: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 39: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 40: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 41: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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;}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 42: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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}

5 6 7 8 4 3 5

40 42 44 46 48 50 52

40

p500

pointer

Page 43: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Arrays of Pointerdata_type *arr[size];

i.e.int *a[7];

char *c[7];

* * * * * * *

40 42 44 46 48 50 52

* * * * * * *

80 82 84 86 88 90 92

Page 44: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

Uses: Sharing & Cost saving

Pass-by-ref

Dynamic Memory Allocation

Making Complex Data Structures Linked list, Tree etc.

Page 45: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

1. Sharing & cost savingvoid main(){

int a=5,b=8;swap(&a,&b);

}void swap(int *a,int *b){

int t = *a;*a = *b;*b = t;

}

Page 46: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

2. DMAvoid 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++;

}}

Page 47: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

3. Data Structures

DataPointer

DataPointer

DataPointer

DataPointer

Page 48: Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers

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.html