Pointer in C/C++

Pointer in C/C++

A Pointer in C/C++ is a special variable that stores the address of another variable. Whenever the address of a variable is assigned to a pointer, we retrieve the address and the value stored in the variable. An ‘ &’ is a reference operator and ‘ * ‘ is a dereference operator.


The pointer declaration is datatype *pointer_name. Example: int *p;

Note: The data type of pointer and the variable must be same. Pointer with int datatype can store address of only int variable. Similarly pointer with float datatype can store address of only float variable, etc.

To store the address of another variable in the pointer we use pointer_name = &variable_name, i.e. p=# This is initialization of pointer.

Then *pointer_name displays the value of the variable. Let’s see an example which shows the use of pointer.

Diagram displaying relation between Pointer and variable.
Relation between pointer and variable

Program showing use of –

1. Pointer in C :

int main()
    int num;
    int *p;
    printf("Enter a number: ");
    printf("Address of the number is %d.\nValue of the number is %d.",p,*p);
    return 0;

2. Pointer in C++ :

using namespace std;
int main()
    int num;
    int *p=&num;
    cout<<"\nEnter a number: ";
    cout<<"Address of the number is "<<p;
    cout<<"\nValue of the number is "<<*p;
    return 0;


Enter a number: 4
Address of the number is 0x61fe14
Value of the number is 4

Note: You can get different address in the output

Some other types of pointer are:

  • Null pointer : A pointer that points to nothing is called a Null pointer. Its syntax is- int *p=NULL;
  • Void pointer : A void pointer is a pointer that is independent of any datatype. It is also known as a general-purpose pointer. Its syntax is- void *p;

Note: When we use a void pointer we have to use (datatype *) to get the address. That is instead of *p we use *(int *)p. Else we get a compiler error.


So a pointer points to the address of the variable. Its use is for the dynamic allocation of memory. We hope we have cleared the basic concept and use of pointers. If you have any doubts do tell us in the comment section below. Share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on Instagram. Visit again!



Leave a Reply

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