top of page

Pointers

Memory is the most important thing in programming.. you cannot do anything without memory. So pointers are important when we work with memory.. You know C/C++. Those are very powerful programming languages. that because we can work with pointers very well in those programming languages.

Most of the times beginners are confused with pointers but there's no rocket science behind this.



figure 1




Just think memory is a one single line of blocks.. As the above picture(figure 1) you can imagine the memory. Like in above picture there is a street and some houses have near to the street, each house has a address and a value. So we can find the house using its address. If you want to deliver a package to a house you can find the house using the address. Memory also like this. Each block of the memory has a memory address, so we can identify each memory address using this its address.

So pointers are use to store these memory address. Pointer is a nothing but a integer. It stores memory addresses. When you are using C/C++ , its very important to know about pointers.


syntax:-

<data type> * <identifier>;


ex:

void* ptr;



#include <iostream>
using namespace std;
int main()
{
    int a=5;
    void* ptr=&a;
    void* p=&a;
    return 0;
}

This is a simple code in C++ that use pointers.


After start the debugging you can see how this pointers are works in the memory.





you can see integer variable a is equal to 5 and its memory address is 0x6dfef4.


Look at following images.






The datatype doesn’t matter in pointers.. We need datatype to manipulate the data in the referenced memory location but pointer is just a integer which stores a memory address.. Don’t confuse..


This is the simple idea of the pointers...


If you want to return a array from a function in C/C++ you cannot return as array.. So you have to return reference. In that case you have to use pointers to get that return value. This is a simple example of uses of the pointers;





35 views0 comments

Recent Posts

See All

Comments


bottom of page