call by ...

Call by value--- part 1 

void test(int i){ 

         i
=5



void main(){ 

         
int i=0

         test(i); 

         cout 
<< "i=" << i << endl; 



the result i
=0  

Call by address
--- part 1 

void test(int *i){ 

         
*i=5



void main(){ 

         
int i=0

         test(
&i); 

         cout 
<< "i=" << i << endl; 



the result i
=5  

Call by reference
--- part 1 

void test(int &i){ 

         i
=5



void main(){ 

         
int i=0

         test(i); 

         cout 
<< "i=" << i << endl; 



the result i
=5  

Call by value
-------- part 2 

void swap(int i,int j){ 

         
int temp; 

         temp
=i; 

         i
=j; 

         j
=temp; 



void main(){ 

         
int i=5

         
int j=10

         swap(i,j); 

         cout 
<< "i=" << i << "j=" << j <<  endl; 



the result i
=5,j=10  

Call by address
----- part 2 

void swap(int *i,int *j){ 

         
int temp; 

         temp
=*i; 

         
*i=*j; 

         
*j=temp; 



void main(){ 

         
int i=5

         
int j=10

         swap(
&i,&j); 

         cout 
<< "i=" << i << "j=" << j <<  endl; 



the result i
=10,j=5  

Call by reference
--- part 2 

void swap(int &i,int &j){ 

         
int temp; 

         temp
=i; 

         i
=j; 

         j
=temp; 



void main(){ 

         
int i=5

         
int j=10

         swap(i,j); 

         cout 
<< "i=" << i << "j=" << j <<  endl; 



the result i
=10,j=5  

Call by value
-------- part 3 

void test(char *q){ 

         q
=(char*)malloc(16); 

         
if(NULL != q) 

         { 

             strcpy(q,
"ABCDE"); 

         } 



void main(){ 

         
char *p=NULL; 

         test(p); 

         
if( p != NULL) 

             cout 
<< "p=" << p << endl; 

         
else 

             cout 
<< "p=NULL"  << endl; 



the result p
=NULL  

Call by address
----- part 3 

void test(char **q){ 

         
*q=(char*)malloc(16); 

         
if(NULL != *q)         { 

             strcpy(
*q,"ABCDE"); 

         } 



void main(){ 

         
char *p=NULL; 

         test(
&p); 

         
if( p != NULL) 

             cout 
<< "p=" << p << endl; 

         
else 

             cout 
<< "p=NULL"  << endl; 



the result p
=ABCDE  

Call by reference
--- part 3 

void test(char *&q){ 

         q
=(char*)malloc(16); 

         
if(NULL != q)         { 

             strcpy(q,
"ABCDE"); 

         } 



void main(){ 

         
char *p=NULL; 

         test(p); 

         
if( p != NULL) 

             cout 
<< "p=" << p << endl; 

         
else 

             cout 
<< "p=NULL"  << endl; 



the result p
=ABCDE

posted @ 2009-02-03 08:57  jerry550409  阅读(190)  评论(0编辑  收藏  举报