C++ logo

The ‘Pointer’ in C++, demystified. [Part-1]

When I first started learning C++, it took me some time to understand the Pointer in C++ and be able to quickly identify Pointer is a variable that point to another variable’s memory address. I found that the hardest thing about understanding the Pointer is that you usually forget the different cases and situations that you have read about or watched people explain during one or more of the C++ tutorials or resources that you study from. I wanted to write this article series to state what I have learned and try to explain it in a way that could help anyone who is learning C++ and having difficulty understanding the Pointer concepts as I did before. I assume that you installed a C++ complier and an IDE in your machine. Before I going to explain Pointer, first of all  type following code and run to observe the output

 

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int var = 20;
    cout<< "value of var is :" << var << "\n";
    cout << "Address of var :" << &var << "\n";
    return 0;
}

Output:

value of var is :20
Address of var :0x7ffeeb3471b8

So, we can see here var has a value and a memory address. When we want to work with variable’s memory address in C++ the  we have to use Pointer. Pointer is a special variable that could contains another variable’s memory address.

Now write following programme and run into your compiler:

 

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int var = 20;
    int *ptr;
    ptr=&var; 
    cout<< " *ptr is = " << *ptr << "\n";
    cout << "Address of ptr :" << ptr<< "\n";
    return 0;
}

Output:

*ptr is = 20
Address of ptr :0x7ffee954f1b8

Notice that , using int *ptr; statement I have declared an Integer type pointer which name is ptr. We could put address of another Integer type variable into ptr and we did it in this line ptr=&var;In this situation if we print ptr then we got address of var. So, how could we get the value of varThe answer is print *ptr.

Note:

1. When we write " int *ptr ", then it should read as "integer pointer ptr".
2. When we write " *ptr ", then it should read as "content of ptr".

I assumed that, you got a quick understanding of Pointer. Now, I am giving here the copy book definition of Pointer:

Pointers are symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures.

Oh.. weird! Isn’t it?

Recap, how to use a pointer?

  • Define a pointer variable
  • Assigning the address of a variable to a pointer using unary operator (&) which returns the address of that variable.
  • Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.

That all for today. In part-2 , I am going illustrate more examples on Pointer concepts, characteristics and their use cases. Thank you and Goodbye till then.

Leave a Reply

Your email address will not be published. Required fields are marked *