Pointers In C++ | Pointer Explained With Real Life Examples | Pointers In OOPs C++

Pointers In C++

What is pointer in c++ ?

If you searched about pointers in c++ on google probably you saw that In C++,pointer refers to a variable that holds the address of another..... BlahBlahBlah.....
But you won't able to understand too much about pointers.
So don't worry I will explain this concept very easily by a real life example.
 
So before understanding about pointers first you have to understand why we need pointers?
As you know basically when you create variable then it will occupy some space in your pc. And when you create a another variable with same value or modify that variable with some other value then it will occupy more space in the pc.
For example - 

#include <iostream>
using namespace std;

int main()
{
    int var = 12; //Suppose This will occupy some space which ID is AB12
    int var1 = 12; //Now This will also occupy some space which ID will be BB12 (ALWAYS DIFFERENT)
    
    var = 45;//This will also occupy some space which ID will be AA11 (ALWAYS DIFFERENT)
    //But Your memory which ID is AB12 (var = 12) will also remain it won't delete
    return 0;
}

I hope you understood why we need pointers. If you don't IDIOT then listen we need to save our pc memory because..........we need free space, you know.

so what pointers do it will simply delete the value of that ID and assign a new one.


So how pointers work let's understand by this real-life example - 

Suppose you have only one box in your kitchen to store something and then first you have samosa then simple you will store in that box right. Now because you are foodie you ate 30 samso in one day then now you need some medicine because you know your stomach will become a atomic bomb chamber and you know what will happen after that. So now you need medicine and you have it but you want to store it in box so you will throw (delete) all the samosa and store medicine in that box.


So same concept is applicable on pointers you have a variable which memory address is BB14 (for example) so now you no longer want the value of that variable so you can delete value in BB14 and you now assign new value in it.
Hope you understood the concept of pointer with this incredible example.

Benefits of using Pointers in C++
Pointers reduce the length and complexity of a program. Pointers allow passing of arrays and strings to functions more efficiently. Pointers make possible to return more than one value from the function. Pointers increase the processing speed.

Now we will see how you can create pointers in C++

#include <iostream>
using namespace std;

int main()
{
    //To define a pointer variable write * at the starting of identifier
   int *p;
   int a = 20; //Suppose this value is stored in an memory which ID is AA12
    
   p = &a; //&a refers address of variable a
   cout<<endl;
   cout<<endl;
   cout<<"ADDRESS OF a VARIABLE: "<<&a<<endl;
   cout<<"VALUE OF p VARIABLE: "<<*p<<endl;
   cout<<"ADDRESS OF p VARIABLE: "<<p<<endl;
   /*
   So here value at adress a (suppose AA12) will be assigned at
   pointer variable p
   so in p the address of &a will be stored
    so the address of p (suppose BB22) now
   */
    int b = 70; // Suppose adress of this variable will CC33
    p = &b;
    cout<<endl;
    cout<<endl;
    cout<<"ADDRESS OF b VARIABLE: "<<&b<<endl;
   cout<<"VALUE OF p VARIABLE: "<<*p<<endl;
   cout<<"ADDRESS OF p VARIABLE: "<<p<<endl;
    /*
    So here value at adress a (suppose CC33) will be assigned at
    pointer varaible p 
    but the address of p will change but the previously 
    valuie or ID of pointer will delete automatically
    
    */


}
 

Output - 



ADDRESS OF a VARIABLE: 0x61ff08
VALUE OF p VARIABLE: 20        
ADDRESS OF p VARIABLE: 0x61ff08


ADDRESS OF b VARIABLE: 0x61ff04
VALUE OF p VARIABLE: 70
ADDRESS OF p VARIABLE: 0x61ff04




Comments

Popular posts from this blog

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example

Function Overloading In C++ | Function Overloading Explained With Real Life Examples | Function Overloading In OOPs