Pointer in C++ with example

Here we will discuss the following,

Pointer in C++

used to access the variable address and manipulate them.

let’s explore it here,

A pointer is a derived data type. In C++ or any other programming language, in the program we first declare the variable to perform any task which contains the input or information store given by the end-user. variable stores these values ​​in the memory.

Now as in memory each information is stored as a byte and these bytes have their own address, so that means which value stored in the memory they have their own address.

Now,  we can access the address of these values ​​using a pointer variable in C++.

So the definition is,

A pointer is a type of variable, which stores or holds the address of values/variable. By using the Pointer variable in C++ we can access the address of any variable and manipulate them.

declaration of a pointer variable

The pointer-variable is like a normal variable, but when we add asterisk (*) to it, it becomes a pointer variable. as follows,


data-type *variable-name;

Here, data can be any data-type like- int, char, float and void data type.

asterisk sign can declare anywhere between data type and variable-name, such as

Data-type* variable-name;

Remember that, there should be the same data type as the data type variable you want to store the address. That is, we cannot store the address of a variable of type char or float with variables of type int i.e-

int x; float *y;
y = &x; // wrong
int x,*y;
y = &x; // right

access variable address using Pointer variable in C++

To access the address of a variable, we use 2 types of operators in Pointer.

  • value of Operator (*)
  • address of Operator (&)

value of Operator

It is used to print store value in Address. It is also called de-reference operator.

int *ptr;
int x = 6;
cout<<*ptr1; // 6

address of operator

It is used to print the address of Value. It is also called Reference operator like-

int *ptr, x = 6;
ptr = &x;
cout<<ptr; // address will be print

without using the de-reference operator (*), we can print the address of a variable, i.e. the address of a variable can also be printed using only the & operator. like,

cout<<&x; // x variable address will be print

using the operator, we can also access the address of the pointer variable itself. like,

cout<<&ptr; // pointer-variable address will be print

This, you can say that we use the pointer variable to store only the address of the variable which other normal variables cannot store.

Here is the complete program, where we user both method,

access variable using Pointer variable

In the program, we have access to the address of variable x in two ways. First, using the pointer-variable, the second one is accessing the variable address using the & operator, then assigning or storing it in a pointer-variable and secondly we have printed the variable directly using the reference operator.

using namespace std;
int main()
   int x = 6,*ptr;
   ptr = &x;

   cout<<"value store the address: "<<*ptr<<endl;
   cout<<"Addres of this variable: "<<ptr<<endl;
   cout<<"\nanother way using &x : "<<&x<<endl;
   cout<<"Addres of pointer variable: "<<&ptr<<endl;
return 0;


value store the address: 6
Addres of this variable: 0x8f29fff2

another way using &x : 0x8f29fff2
Addres of pointer variable: 0x8f29fff0

Note : Pointer is used for accessing the address of a variable, as well as for dynamic memory allocation.

more about pointer

Previous- Function and Their Types in C++
Next- memory allocation in C++

Like it?
Exit mobile version