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. 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.
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. 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. • 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. • 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. 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 a
When declaring a pointer that points to another pointer,
2 asterisks must precede the pointer name
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. //Array declaration:
• int array[5] = {1,2,3,4,5};
//Accessing array elements:
Array-indexing Pointer notation
array[0] *array 1st element
array[1] *(array+1) 2nd element
array[2] *(array+2) 3rd element
array[3] *(array+3) 4th element
array[4] *(array+4) 5th element
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 ?
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. 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. Differences between Pointers
and References
RESTRICTIONS Reference Pointer
It reserves a space in the memory to store NO YES
an address that it points to or references.
It has to be initialized when it is declared YES NO
It can be initialized at any time NO YES
Once it is initialized, it can be changed to NO YES
point to another variable of the same type
It has to be dereferenced to get to a value NO YES
it points to
It can be a NULL pointer/reference NO YES
It can point to another reference/pointer NO YES
An array of references/pointers can be NO YES
created
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. Quiz
//Program demonstrates a use of an independent reference variable.
int 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. Lab Work
• Passing references to functions
• Returning references by functions
Next Class
• Using references and pointers with
constants
• Dynamic memory allocation