290 likes | 566 Views
A presentation on. POINTERS IN C++. Outlines R. Objective Introduction Operators Pointers & Arrays Pointers & Functions Pointers to pointers Allocation & Deleting pointers (optional) Questions & Answers. Objective.
E N D
A presentation on POINTERS IN C++
Outlines R • Objective • Introduction • Operators • Pointers & Arrays • Pointers & Functions • Pointers to pointers • Allocation & Deleting pointers (optional) • Questions & Answers
Objective The objective is to provide an insight to pointers. As they R considered a bit Difficult.
Introduction • Variables to store memory address • Acts as “third party” • Indirect access to variables • A sketch of memory
Why R they Used • Managing data on memory • Accessing class members data • Passing variables by reference • To pass Arrays and strings more easily • To create complex data structures • ………………………………………………
Getting Started • Declaration • Initialization • Names of pointers
Types of pointers • Null pointers • Wild/stray/Dangling pointers • Const. pointers
OPERATORS In case of Pointers ,two types of operators R used Address operator Value at address operator
Address/Reference Operator( & ) Also called reference operator, indicated as” & “ Used to get address of any location of memory
int n=55; cout<<&n; OUTPUT 0X00ffde27 EXAMPLE 1
int c=20; int k=99; float d=234; char oper=‘ali’; cout<<&c<<endl<<&k; cout<<&d<<endl<<&oper OUTPUT 0x0064ff54 0x0064ff56 0x0064ff58 0x0064ff62 EXAMPLE2
int *p, x=15; P=&x; cout<<“Address of x“<<p; OUTPUT 0xde56ff07 EXAMPLE 3
Dereferencing/Value at address operator “*” operator is named as Dereferencing operator. It is also called Indirection operator, used to indicate pointers and also value at that address
EXAMPLE 1 int * p1; float * p2; 0xddfe3476
POINTERS & ARRAYS Pointers pass arrays more easily. These pointer Arrays can store addresses of related items.
EXAMPLE 1 void main() { short a[ ]={22,33,44,55} cout<<“ a= “<<a<<“ *a ”<<*a<<endl; for(short *p=a ;p<a+4 ;p++) cout<<“p=“<<p<<“*p=“<<*p; getch(); }
POINTERS & FUNCTIONS A simple pointer to a function is a simple pointer, whose value is address of function name.
EXAMPLE void fun1( int *p ,int *q){ int c; c=*p; *p=*q; *q=c; cout<<*p<<*q; } void main (){ int x ,y; cin>>x>>y; fun1 (&x ,&y); //reference type parameters getch();}
pf f Int f (int n) { --------------- --------------- }
POINTERSTOPOINTERS This is possible that a pointer may direct to another pointer
EXAMPLE Char c=‘w’; Char *pc=&c; Char **ppc=&pc; Char ***pppc=&ppc; ***pppc=‘w’;
pppc ppc Pc c Pointer to a pointer t
ALLOCATION & DELETING Pointers can be allocated as well as deleted
int *p;//static allocation P=new int;//Dynamic Allocation delete p; EXAMPLES
CAUTIONS • NEVER delete a deleted pointer • NEVER call a deleted pointer • ALWAYS set a deleted pointer to null for safety
EXAMPLE delete *p; P=&c; //wrong …………………………… delete x; Y=12; D=34; delete x; //wrong
EXAMPLE int x=6,y=44; int *p1=&x,*p2=&y; cout<<“p1before deleting=“<<*p1; delete p1; p1=new int; *p1=911; Cout<<“after deleting p1 is=“<<*p1; getch();
Output P1 before deleting =6 P1 after deleting =911