1 / 29

A presentation on

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.

reidar
Download Presentation

A presentation on

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A presentation on POINTERS IN C++

  2. Outlines R • Objective • Introduction • Operators • Pointers & Arrays • Pointers & Functions • Pointers to pointers • Allocation & Deleting pointers (optional) • Questions & Answers

  3. Objective The objective is to provide an insight to pointers. As they R considered a bit Difficult.

  4. Introduction • Variables to store memory address • Acts as “third party” • Indirect access to variables • A sketch of memory

  5. 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 • ………………………………………………

  6. Getting Started • Declaration • Initialization • Names of pointers

  7. Types of pointers • Null pointers • Wild/stray/Dangling pointers • Const. pointers

  8. OPERATORS In case of Pointers ,two types of operators R used Address operator Value at address operator

  9. Address/Reference Operator( & ) Also called reference operator, indicated as” & “ Used to get address of any location of memory

  10. int n=55; cout<<&n; OUTPUT 0X00ffde27 EXAMPLE 1

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

  12. int *p, x=15; P=&x; cout<<“Address of x“<<p; OUTPUT 0xde56ff07 EXAMPLE 3

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

  14. EXAMPLE 1 int * p1; float * p2; 0xddfe3476

  15. POINTERS & ARRAYS Pointers pass arrays more easily. These pointer Arrays can store addresses of related items.

  16. 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(); }

  17. POINTERS & FUNCTIONS A simple pointer to a function is a simple pointer, whose value is address of function name.

  18. 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();}

  19. pf f Int f (int n) { --------------- --------------- }

  20. POINTERSTOPOINTERS This is possible that a pointer may direct to another pointer

  21. EXAMPLE Char c=‘w’; Char *pc=&c; Char **ppc=&pc; Char ***pppc=&ppc; ***pppc=‘w’;

  22. pppc ppc Pc c Pointer to a pointer t

  23. ALLOCATION & DELETING Pointers can be allocated as well as deleted

  24. int *p;//static allocation P=new int;//Dynamic Allocation delete p; EXAMPLES

  25. CAUTIONS • NEVER delete a deleted pointer • NEVER call a deleted pointer • ALWAYS set a deleted pointer to null for safety

  26. EXAMPLE delete *p; P=&c; //wrong …………………………… delete x; Y=12; D=34; delete x; //wrong

  27. 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();

  28. Output P1 before deleting =6 P1 after deleting =911

  29. QUESTIONS & ANSEWRS

More Related