Date post: | 18-Dec-2014 |
Category: |
Education |
Upload: | bilal-ahmed |
View: | 15 times |
Download: | 0 times |
Introduction to Introduction to ProgrammingProgramming
Lecture 30Lecture 30
In Today’s In Today’s Lecture Lecture
– ReferencesReferences– Differences Differences
Between References Between References
and Pointersand Pointers
ReferenceReference
&&
int & i ; int & i ; ‘i’ is a reference to an integer‘i’ is a reference to an integerint * i ;int * i ; ‘i’ is a pointer to an integer‘i’ is a pointer to an integer
int i ;int i ;int & j = i ;int & j = i ;
Example Example 11main ( )main ( )
{{
int i ;int i ;
int & j = i ;int & j = i ;
i = 123 ;i = 123 ; Output Output
cout << i << endl ;cout << i << endl ; 123123
cout << j << endl ;cout << j << endl ; 123123
i++ ;i++ ;
cout << i << endl ;cout << i << endl ; 124124
cout << j ;cout << j ; 124124
}}
Example 2Example 2void swap ( int x , int void swap ( int x , int y )y )
{{int temp = x ; int temp = x ; x = y ; x = y ; y = temp ;y = temp ;
}}
Example 2Example 2main ( )main ( ){{
int x , y ;int x , y ;x = 10 ;x = 10 ;
y = 20 ;y = 20 ;swap ( x , y ) ;swap ( x , y ) ;cout << x << y ;cout << x << y ;
}}
Example 2Example 2main ( )main ( ){{
int x , y ;int x , y ;x = 10 ; y = 20 ;x = 10 ; y = 20 ;swap ( & x , & y ) ;swap ( & x , & y ) ;cout << x << y ;cout << x << y ;
}}
swap ( int * i , int * swap ( int * i , int * j ) ;j ) ;
void swap ( int * i , int * j )void swap ( int * i , int * j )
{{
int * temp = i ; int * temp = i ;
* i = * j ; * i = * j ;
* j = * temp ;* j = * temp ;
}}
Example 2Example 2
void swap ( int & i , int & j )void swap ( int & i , int & j )
{{
int temp = i ; int temp = i ;
i = j ; i = j ;
j = temp ;j = temp ;
}}
Example 3Example 3
Example 3Example 3main ( )main ( ){{int x , y ;int x , y ;x = 10 ;x = 10 ;
y = 20 ;y = 20 ;swap ( x , y ) ;swap ( x , y ) ;cout << x << y ;cout << x << y ;
}}
constconst
Example 4Example 4struct bstruct b
{{
int serno ;int serno ;
char text [ 1000 ] ; // A lot of charschar text [ 1000 ] ; // A lot of chars
} b = { 123, "This is a } b = { 123, "This is a BIGBIG structure" } ; structure" } ;
void valfunc ( b ) ; // Call by valuevoid valfunc ( b ) ; // Call by value
void ptrfunc ( * b ) ; // Call by pointervoid ptrfunc ( * b ) ; // Call by pointer
void reffunc ( & b ) ; // Call by referencevoid reffunc ( & b ) ; // Call by reference
void main ( )void main ( ){{ valfunc ( b ) ; valfunc ( b ) ; // Passing the variable itself// Passing the variable itself
ptrfunc ( & b ) ; ptrfunc ( & b ) ; // Passing the address of the variable// Passing the address of the variable
reffunc ( b ) ; reffunc ( b ) ; // Passing a reference to the variable// Passing a reference to the variable
}}
Example 4Example 4
Example 4Example 4struct bstruct b{{ int serno ; int serno ; char text [ 1000 ] ; // A lot of charschar text [ 1000 ] ; // A lot of chars} b = { 123 , "This is a BIG structure" } ;} b = { 123 , "This is a BIG structure" } ;
void valfunc ( b ) ; // Call by valuevoid valfunc ( b ) ; // Call by value
void ptrfunc ( const * b ) ; // Call by pointervoid ptrfunc ( const * b ) ; // Call by pointer
void reffunc ( const & b ) ; // Call by referencevoid reffunc ( const & b ) ; // Call by reference
Difference Between Difference Between References and References and
PointersPointers
main ( ) main ( )
{{
int i ;int i ;
int & refi = i ;int & refi = i ;
cout << “The address of the cout << “The address of the reference is reference is
” << & refi ;” << & refi ;
cout << & i ;cout << & i ;
}}
Dangling Dangling ReferencReferenc
ee
References as Return References as Return ValuesValuesint mynum = 0 ; // Global variableint mynum = 0 ; // Global variable
int & num ( )int & num ( ){{ return mynum ;return mynum ;}}void main ( )void main ( ){{ int i ;int i ;
mynum = 100 ;mynum = 100 ;i = num ( ) ;i = num ( ) ;
num ( ) = 200 ;num ( ) = 200 ;}}
int & i ;int & i ;
double & f ;double & f ;
char & c ;char & c ;
a = b = c ;a = b = c ;
a = b ;a = b ;
cout << “The values of the cout << “The values of the
integer is ”<< i << endl ; integer is ”<< i << endl ;