Chp3(pointers ref)


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chp3(pointers ref)

  1. 1. Pointers andReferences
  2. 2. OBJECTIVES• To understand the differences between C and C++ pointers• To be able to use references as independent variables and function parameters• To be able to use pointers and references with constants
  3. 3. INTRODUCTION• C pointers are very powerful. If not used properly, prone produce unpredictable results, system clashes.• C++ enhances C pointers, provides increases security because of its rigidity.• References have advantages over regular pointers when passed to functions.
  4. 4. C++ POINTERS
  5. 5. REVIEWING THE FUNDAMENTALS OF POINTERS• A pointer : variable used to store a memory address.• The address : location of one of the following in memory:- - variable, pointer, function• Major benefits: - support dynamic memory allocation - functions can modify actual arguments - support data structures : linked-list, binary trees - improve efficiency of some programs• To avoid system crashes, use pointers correctly!
  6. 6. Data_type *variable_name;• int *ptr1; //point to any variable of type integer• double *ptr2; //point to any variable of type double• void *ptr3; //point to a variable of any type• Robot *ptr4; //point to any variable of user- defined type named Robot.(*) indirection operator (&) address-of operator• & returns memory address of its operand• * precedes a pointer and returns the value of a variable, the address of which is stored in the pointer
  7. 7. • float x = 1.23, y;• float *pt; //pt can point to any variable of type float• pt=&x; //places the memory address of x into pt• cout<<*pt; //prints the value of x : 1.23• * (access value the pointer points to) - dereferencing the pointer.• A void pointer cannot be dereferenced.• void *pt1; //pt1 is a void pointer• int *pt2; //pt2 is an integer pointer• int x = 3, y, z;• pt1 = pt2 = &x; //pt1, pt2 point to x• y=*pt1; //Error! pt1 cannot be dereferenced• z=*pt2; //Correct! pt2 is dereferenced.Value of x is placed into z
  8. 8. • Pointers can be used as operands in assignments, arithmetic, comparison expressions.• float f=13.3; //Assume: address of f is 1000 and size of float is 8 bytes.• float *ptr1, *ptr2; ptr1 = &f;• ptr2=ptr1; //Assigning pointers: ptr1 and ptr2 point to f• ptr1--; //Decrementing ptr1• cout<<ptr1; //value of ptr1 is 992 (=1000-8)• ptr2=ptr2+5; //adding 5 to ptr2 and assigning result to ptr2• cout<<ptr2; //value of ptr2 is 1040 (=1000+5*8)• if (ptr1==ptr2) //comparing pointers by equality cout<<“Both pointers contain same memory address.”;
  9. 9. A pointer can point to another pointer• float a=0.99, *b, **c;• b=&a; //pointer b points to pointer a• c=&b; //pointer c points to pointer b• cout<<**c; //deferencing pointer c two times to access aWhen declaring a pointer that points to another pointer, 2 asterisks must precede the pointer name
  10. 10. Array• Array elements can also be accessed using pointers.• Array name returns starting address of the array (address of 1st element of array)• Array name can also be used as a pointer to the array.• There are 2 different ways of accessing array elements.• Accessing array elements using pointers can be faster than array indexing.
  11. 11. //Array declaration:• int array[5] = {1,2,3,4,5};//Accessing array elements:Array-indexing Pointer notationarray[0] *array 1st elementarray[1] *(array+1) 2nd elementarray[2] *(array+2) 3rd elementarray[3] *(array+3) 4th elementarray[4] *(array+4) 5th element
  12. 12. String• A string is equivalent to a character pointer.• Operations with strings are often performed by using pointers.• char scientist[13] = “Nikola Tesla”• char *cptr;• cptr=scientist; //cptr is set to the address of scientist• cout<<*(cptr+2); //prints the 3rd character of?• cout<<cptr; //prints the ?
  13. 13. REFERENCES
  14. 14. • C++ provides new kinds of variables called references• Acts as alternatives names for other variable• Can be used in 3 different ways: - independent variable - passed to a function - returned by a function• Passing references between function is a powerful and very important use of references.
  15. 15. REFERENCES as Independent Variables• Data_type & reference_name = variable_name float num = 7.3; float & refnum = num; //No & before num //space between & and refnum: optional
  16. 16. Differences between Pointers and ReferencesRESTRICTIONS Reference PointerIt reserves a space in the memory to store NO YESan address that it points to or references.It has to be initialized when it is declared YES NOIt can be initialized at any time NO YESOnce it is initialized, it can be changed to NO YESpoint to another variable of the same typeIt has to be dereferenced to get to a value NO YESit points toIt can be a NULL pointer/reference NO YESIt can point to another reference/pointer NO YESAn array of references/pointers can be NO YEScreated
  17. 17. • Because of the practical reasons and restrictions mentioned in previous, most programmers feel that there is no reason to use references as independent variables.• They can also add complexity to a program.
  18. 18. Quiz//Program demonstrates a use of an independent reference main(){ int i = 13; int &iref = i; cout<<“The value is => “<<iref; i--; cout<<“nAfter decrementing => “<<iref<<endl; iref = 99; cout<<“The value is now => “<<i; return 0;}
  19. 19. Lab Work• Passing references to functions• Returning references by functions Next Class• Using references and pointers with constants• Dynamic memory allocation