Simple way to understand pointer

We all know that pointer is a variable that store the address of another variable and pointer itself has an address in the memory.

Let pointer variable ptr be a box that is pointed to another location of a variable.

int n;
int *ptr = &n;


Pointer variable ptr is pointing to location 2346 in the memory. Here the value of ptr will be 2346. Ok now let's see another diagram.

n = 32;


Here memory locations are 2342, 2343, 2344, 2345, 2346.... Pointer ptr is pointing memory location 2345. 32 is the value that is stored in variable n. Memory location 2345 will be the value of pointer variable ptr.

Now if we change the value stored in n, then value stored at location 2345 will be changed.

n = 65;


The value stored in variable n is changed but pointer value will not change. Now we change value at location 2345 using pointer.

*ptr = 98;

Then value at location 2345 will be 98.

Now we declare another variable m.

int m = 12;
ptr = &m;

And we are pointing ptr to m and suppose memory location of m is 2347. So the value at location 2347 will be 12.

pointer value, change pointer value, pointer in c++


Here value of ptr will be 2347. If we do something like this.

*ptr = 13;

Then the value at location 2347 will be changed.

If we do something like this.

int *ptr2 = 15;

It is wrong because pointer variable ptr2 is not pointing any location.

pointer, c language pointer, understand pointer, c++ pointer, programming language pointer






Popular posts from this blog