Sunday, 15 January 2017

[C++] return by address/reference


 int *createArrayAddr(int n) {  
   int *a = new int(5);  
     
   cout << (void*) a << endl;  
     
   return a;  
 }  
   
 int &createArrayRef(int &n) {  
     
   cout << &n << endl;  
     
   return n;  
 }  
   
 int main(int argc, const char * argv[]) {  
   int *a = createArrayAddr(5);  
   cout << (void*) a << endl;  
     
   int n = 55;  
   cout << &n << endl;  
   int &n2 = createArrayRef(n);  
   cout << &n2 << endl;  
   
   return 0;  
 }  

RESULT:

 0x1001054d0  
 0x1001054d0  
 0x7fff5fbff7f4  
 0x7fff5fbff7f4  
 0x7fff5fbff7f4  

No comments:

Post a Comment