Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Pointer(*) Basics

Pointer is the most confusing topic in Functional and Structural Programming.Why not it's one of most generic interview Topic as well as choosen topic for every Programmer.I believe after Reading this, your all doubts related to Pointer will be cleared.

Pointer Definition

Let's declare a Variable

 int main(){
    int a=5;  //It eats up some memory in your RAM. Cause you've declared a variable.
    }

Since each statement we declare in our program eats up some memory and in RAM the memory is divided into some random block which is called Memory block.So the variable a=5 has taken 4 bytes of Memory in the RAM memory Block.These Memory Block have some addresses What if we are interested in knowing that address?

Let's know the address of the variable.

 int main(){
   int a=5;
   cout<<&a<<endl; // 
   }

The output will be some 0x Decimal Value and depend on your Operating System.

What if we want to store this address into some variable?

 int main(){
    int a=5;
    int b= &a;   // Will it work??? It will throw an error but why??
    cout<<b<<endl;
  }

The above program will throw an error because Memory Address changes every time when you compile the Program. And when you compile this program each time memory address changes with some random memory blocks and we can not store the random address into an variable. So whats the next Option. Here comes Pointer.

Poiner- Pointer is an variable which helps to store the address of another variable.

And now our program will run smoothly,

 int main(){
    int a=5;
    int *ptr= &a;
    cout<<ptr<<endl;
   }