Pointer Vs Reference variable in C/C++

Pointer Vs Reference
Pointer Vs Reference

As a beginner Most of the time one gets confused, if both are referring to the same things then what exactly they are. Let’s find out with a real-world scenario.

We all have a real name and a calling name by which your friend might call you and an address where we live. So calling name is a reference to me, your can call me by my real name as well as calling name. The address is the pointer where I live. So to meet me you have to either call me by name or calling name or use address to visit my house.

Advertisements
#include <iostream>
using namespace std;

int main(){
    string name = "Aditya" ;     // Real Name 
    string &adii = name;         // Calling name used by my friend 
    string *ptr  = &name;        // My address 0x7ffe8ab9a320

//<-------------Some Codes are in below section ----------------->
return 0;
}

You have three ways to meet me, in terms of the program you can access a variable in three different ways.

Note :- Reference variable must be initialized before compile , else who knows that you are know by other name too.

std::cout<<"Actual Name is " <<name<<endl;
std::cout<<"Reference Name is "<<adii<<endl; 
std::cout<<"My Address is "<<ptr<<endl;
std::cout<<"Who lives at that address "<<*ptr<<endl;
Output
Actual Name is:- Aditya
Reference Name is :- Aditya
My Address is at:- 0x7ffe8ab9a320
Who lives at that address:- Aditya

Now you have to meet me in three different ways, why not change/Modify info.

//First you can directly modify using actual name  
name = "Aditya G";
std::cout<<name<<endl;
// output -> Aditya G

//second ,using reference name 
adii = "A Gaurav";
std::cout<<adii<<endl;
//output -> A Gaurav

//thrid, Using Pointer(Address)
*ptr = "Aditya Gaurav";
std::cout<<*ptr<<endl;
//output -> Aditya Gaurav 

// Now even if you access variable by first two ways you will find out info is updated everywhere.
std::cout<<adii<<endl;
//output -> Aditya Gaurav
std::cout<<name<<endl;
//output -> Aditya Gaurav

Even if you forget by now you can remember it as Your Name is a variable, your calling name is reference [Your friends used to call you] and your address is pointer by which people come to meet you from remote.

Advertisements
Advertisements
Advertisements

11 comments

  1. I do love the manner in which you have presented this problem plus it does indeed give us a lot of fodder for thought. However, through everything that I have seen, I basically hope as the opinions stack on that individuals keep on point and not get started on a soap box regarding the news du jour. Anyway, thank you for this outstanding piece and whilst I can not necessarily agree with this in totality, I respect your standpoint.

    Liked by 1 person

  2. fantastic submit, very informative. I’m wondering why the other experts of this sector don’t understand this. You should proceed your writing. I’m confident, you have a huge readers’ base already!

    Liked by 1 person

  3. It抯 really a great and helpful piece of info. I抦 satisfied that you simply shared this useful information with us. Please keep us up to date like this. Thank you for sharing.

    Like

Leave a reply to Hairstyles Cancel reply