Pointers and references in C++

In this tutorial, we will learn about pointers and references in C++.
Pointers are one of the powerful features of C++. Pointers are used to access and manipulate addresses.

Everything you need to know about pointers and references in C++

We are going to learn the following :

  •  How to declare pointers?
  • What are references? And what is dereferencing?
  • Examples on pointers.

Pointers are variables that point to addresses of some other variable(s).
But before that, to find out the address of a variable, we use the &(reference) operator. It returns the address in which the variable is stored. It varies on each machine, with every time the program executed.

To give you an idea, here is a program to find out the address of a variable :

#include<iostream>
using namespace std;
int main()
{
  int var1 = 10;
  int var2 = 15;
  int var3 = 20;
  cout<<&var1<<endl;
  cout<<&var2<<endl;
  cout<<&var3<<endl;
  return 0;
}

Output – 

0x72fe4c
0x72fe48
0x72fe44

Note : The 0x in the starting tells us that it is in Hexadecimal form.

Getting back to pointers; to declare a pointer, we use * operator. Pointers are declared as follows –

int *p;
  or   
int* p;

The statement above declares a pointer variable p. It holds the memory address of a integer variable.
We can declare a different data type pointer too, just by changing the data type in the above statement to the appropriate data type. Asterisk is a deference pointer, which means ‘pointer to’.

What are references?

We can assign address of a variable to another variable(pointer) by using &’ operator. By using this operator, we can assign the address of a variable of the same data type to same data type pointer only. Otherwise, there is ‘type mismatch’ error.

To assign, the address of a variable to a pointer, it can be done in this way –

int a = 10;
int *pointer;
pointer = &a;
cout<<"The value of a is "<<*pointer<<endl;

Here, the pointer pointer holds the address of the variable a, which might be something like 0x72fe44(just an example). To get the value of a that is stored at the address held by the pointer, we use the dereferencing operator (*) . This operator fetches the value stored at the address.

int val = *pointer;

This stores the value of pointer , which is 10, into the variable val.

Examples on pointers

Here are some examples on pointers –

1.

#include<iostream>
using namespace std;
int main()
{
  int var = 10;
  int *ptr;
  cout<<"Value of var is: "<<var<<endl;	
  cout<<"Address of var is: "<<&var<<endl;	
  ptr = &var;
  cout<<"Value of ptr is: "<<ptr<<endl;	
  cout<<"Address of ptr is: "<<&ptr<<endl;
  cout<<"The value stored at address "<<ptr<<" is: "<<*ptr<<endl;
  return 0;
}

Output – 

Value of var is: 10
Address of var is: 0x72fe3c
Value of ptr is: 0x72fe3c
Address of ptr is: 0x72fe30
The value stored at address 0x72fe3c is: 10

2.

// more pointers
#include <iostream>
using namespace std;

int main ()
{
    int first = 5, second = 15;
    int * p1, * p2;

    p1 = &first;   // p1 = address of first
    p2 = &second;  // p2 = address of second
    *p1 = 10;      // value pointed to by p1 = 10
    *p2 = 20;      // value pointed to by p2 = 20
    cout << "first is: " << first <<endl;
    cout << "second is: " << second <<endl;
    p2 = p1;       // p1 = p2 (value of pointer is copied)
    *p1 = 40;      // value pointed to by p1 = 20
    cout << "first is " << first <<endl;
    cout << "second is " << second <<endl;
    return 0;
}

Output – 

first is: 10
second is: 20
firstvalue is 40
secondvalue is 20

 

Continue reading more articles:

Leave a Reply

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