C++ Pointers


Go to main Page

Pointers in C++

In this tutorial of C++ we are going to learn about Pointers with the help of examples.

Defination: In C++ Programming Pointers are the variables that are used to store the address of other variables


Address in C++

If you have a variable a we can find the address by writing &a .

Example

#include <iostream>
using namespace std;

int main()
{
    
    int a1 = 3;
    int a2 = 24;
    int a3 = 17;
    cout << "Address of a1: "<< &a1 << endl;

    cout << "Address of a2: " << &a2 << endl;


    cout << "Address of a3: " << &a3 << endl;
}

Output

Address of var1: 0x7fff5fbff8ac
Address of var2: 0x7fff5fbff8a8
Address of var3: 0x7fff5fbff8a4


Declaration of Pointers

Syntax

int *pointVar;


Example : Working of C++ Pointers

#include <iostream>
using namespace std;
int main() {
    int var = 5;
int* pointVar;
pointVar = &var;


    cout << "var = " << var << endl;

    cout << "Address of var (&var) = " << &var << endl
         << endl;
    cout << "pointVar = " << pointVar << endl;

    cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar << endl;
    
    return 0;
}

Output

var = 5
Address of var (&var) = 0x61ff08

pointVar = 0x61ff08
Content of the address pointed to by pointVar (*pointVar) = 5