Thread: main use of references

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    main use of references

    I wrote c++ program to understand main use of references in the program

    Code:
    #include<iostream> using namespace std; 
      
    int main() 
    { 
      int x = 10;  
      cout << "Location of x = " << &x << endl ; 
      
      int& ref = x;  
      cout << "Location of ref = " << &ref << endl ;
      
      ref = 20; //set ref to 20
      cout << "x = " << x << endl ; 
      cout << "Location of x = " << &x << endl ; 
      cout << "Location of ref = " << &ref << endl ;
     
      x = 30; // set x to 30 
      cout << "ref = " << ref << endl ;
      cout << "Location of x = " << &x << endl ; 
      cout << "Location of ref = " << &ref << endl ;  
      
      return 0; 
    }
    Location of x = 0x61ff18
    Location of ref = 0x61ff18
    x = 20
    Location of x = 0x61ff18
    Location of ref = 0x61ff18
    ref = 30
    Location of x = 0x61ff18
    Location of ref = 0x61ff18

    I can't figure out result x and ref variable have same address how it's possible.

    I have read pointer and reference are separate both store memory location what are the main difference between pointer and reference

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the FAQs here: references
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    The FAQs or any other reliable source as laserlight mentions. If your studying from a book that doesn't teach you the differences and uses clearly, you need to download a PDF to refer to or buy a "good" beginner friendly book.

    And, if you're looking for real life examples of where references are used, they are used almost everywhere when it comes to writing efficient and optimised code.

    Pointers just point to memory locations. References create aliases i.e. different names referring to the same thing for your already existing variables. You use references to prevent additional/redundant copying that may impact performance.

    > I can't figure out result x and ref variable have same address how it's possible.

    Again, references create aliases aka different names referring to the same thing. So x and ref refer to the same memory location. Hence, what you see.

    Pointers vs References in C++ - GeeksforGeeks
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Zeus_ View Post
    The FAQs or any other reliable source as
    And, if you're looking for real life examples of where references are used, they are used almost everywhere when it comes to writing efficient and optimised code.
    I tried to understand with my code
    pass by pointer
    Code:
    #include<iostream> using namespace std; 
    
    
    void passbypointer(int* x)
    {
    	*x = 20;
    }	
    int main() 
    { 
      int a = 10;  
      passbypointer(&a);
      cout << "a = " << a << endl ; 
      
      return 0; 
    }
    pass by reference

    Code:
    #include<iostream> using namespace std; 
    
    
    void passbyreferences(int& x)
    {
    	x = 20;
    }	
    int main() 
    { 
      int a = 10;  
      passbyreferences(a);
      cout << "a = " << a << endl ; 
      
      return 0; 
    }
    but I can't figure out difference between both because they are working in similar way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, there's no difference. No difference at all.

    Let's move on. Pointers and references are the same thing. Yeah, that's right.

    ... or you could actually read the material that people take the trouble to link you to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    Yeah, there's no difference. No difference at all.

    Let's move on. Pointers and references are the same thing. Yeah, that's right.

    ... or you could actually read the material that people take the trouble to link you to.
    okay understood

    -> A reference must always refer to something. Null are not allowed
    -> A reference must be initialized when it is created
    ->once initialized it can not be change with another variable

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    47
    here is my small input

    anyone can correct me if iam wrong
    Code:
    #include<iostream>
    
    
    
    using namespace std;
    
    
    
    
    int main(){
    // Variable declaration
     int x = 10;
     
     // Pointer declaration
     int* ref = &x;
    
    
     //output the value of x
     cout<<x<<endl;
    
    
    
    
    // Reference: Output the memory address of x
    
    
     cout<<ref<<endl;
    
    
     // Need to Dereference: Output the value of x before assigning new value
    
    
     cout<<*ref<<endl;
    
    
     // Change the value of the pointer
     *ref =20;
     // Output the new value of the pointer
     cout<<*ref<<endl;
    
    
     // Output the new value of the variable x
     cout<<x<<endl;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  2. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  3. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM

Tags for this Thread