Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Pointers

In this tutorial, we will learn about pointers in C++ and their working with the help of examples.


Pointers

  • Pointers are variables that store the memory addresses of other variables.
  • Pointers are symbolic representation of address.
  • Pointers are one of the best features of c++ in comparison to other programming languages like- java, python etc.
  • They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures.
  • A pointer holds the address of an object stored in memory. The pointer then simply “points” to the object. The type of the object must correspond with the type of the pointer.

Address in C++

If we have a variable var in our program, &var will give us its address in the memory.


Example 1: C++ Display the Variable Addresses

#include <iostream>
using namespace std;
int main(){
    // declare variables
    int var1 = 2;
    int var2 = 4;

    // display the address of var1
    cout << "The Address of var1: "<< &var1 << endl;

    // display the address of var2
    cout << "The Address of var2: " << &var2 << endl;
}

Output

The Address of var1: 0x7ffdd6999d20
The Address of var2: 0x7ffdd6999d24

Working

  • Here, 0x at the beginning represents the address is in the hexadecimal form.
  • Notice that the first address differs from the second by 4 bytes.
  • This is because the size of an int variable is 4 bytes in a 64-bit system.

Note: You may not get the same results when you run the program.


How to declare a Pointer ?

As mentioned above, pointers are used to store addresses rather than values.

Syntax:

data_type *pointer_name;

Here is how we can declare pointers.

int *pVar;

Here, we have declared a pointer pVar of the int type.


Rules for declaring a pointer

  • The data type is the base type of the pointer which must be a valid C++ data type.
  • The variable_name is should be the name of the pointer variable.
  • Asterisk * used above for pointer declaration is similar to asterisk used to perform multiplication operation. it is the asterisk that marks the variable as a pointer.

We can also declare pointers in the following way.

int* pVar; // preferred syntax

Let’s take another example of declaring pointers.

int* pVar, p;

Here, we have declared a pointer pVar and a normal variable p.

Note: The * operator is used after the data type to declare pointers.


Assigning Addresses to Pointers

Here is how we can assign addresses to pointers:

int* pVar, a;
a = 2;

// assign address of a to pVar pointer
pVar = &a;

Here, 2 is assigned to the variable a. And, the address of a is assigned to the pVar pointer with the code pVar = &a.

When & is used with pointers, it’s called the reference operator. It returns the variable’s address.


Get the Value from the Address Using Pointers

To get the value pointed by a pointer, we use the * operator. For example:

int* pVar, a;
a = 2;
// assign address of a to pVar
pVar = &a;

// access value pointed by pointVar
cout << *pVar << endl;   // Output: 2

In the above code, the address of a is assigned to pVar. We have used the *pVar to get the value stored in that address.

When * is used with pointers, it’s called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *pVar = a.

Note: In C++, pVar and *pVar is completely different. We cannot do something like *pVar = &a;

Example 2: Working of C++ Pointers

#include <iostream>
using namespace std;
int main() {
    int a = 2;

    int* pVar;    // declare pointer variable

    pVar = &a; // store address of a
   
    cout << "a = " << a << endl; // print value of a

    cout << "Address of a (&a) = " << &a << endl;     // print address of a

    cout << "pVar = " << pVar << endl;    // print pointer pVar

    // print the content of the address pVar points to
    cout << "The content of the address pointed to by pVar (*pVar) = " << *pVar << endl;
    
    return 0;
}

Output

a = 2
Address of a (&a) = 0x7ffc3d579a0c

pVar = 0x7ffc3d579a0c
The content of the address pointed to by pVar (*pVar) = 2

Changing Value Pointed by Pointers

If pVar points to the address of a, we can change the value of a by using *pVar.

For example,

int a = 2;
int* pVar;

pVar = &a; // assign address of a

*pVar = 4; // change value at address pVar
cout << a << endl; // Output: 4

Here, pVar and &a have the same address, the value of a will also be changed when *pVar is changed.


Example 3: Program to display Changing Value Pointed by Pointers

#include <iostream>
using namespace std;
int main() {
    int a = 2;
    int* pVar;
   
    pVar = &a; // store address of a

    // print a
    cout << "a = " << a << endl;

    // print *pVar
    cout << "*pVar = " << *pVar << endl;

    cout << "Changing value of a to 4:" << endl;
    
    a = 4; // change value of a to 4

    // print a
    cout << "a = " << a << endl;

    // print *pVar
    cout << "*pVar = " << *pVar << endl;

    cout << "Changing value of *pVar to 6:" << endl;

    // change value of a to 6
    *pVar = 6;

    // print a
    cout << "a = " << a << endl;

    // print *pointVar
    cout << "*pVar = " << *pVar << endl;
    return 0;
}

Output

a = 2
*pVar = 2
Changing value of a to 4:
a = 4
*pVar = 4
Changing value of *pVar to 6:
a = 6
*pVar = 6

Common Errors when using pointers

Suppose, we want a pointer pVar to point to the address of a. Then,

int a, *pVar;
                                                                      
// Wrong! 
// pVar is an address but a is not
pVar = a;

// Wrong!
// &a is an address
// *pVar is the value stored in &a
*pVar = &a;

// Correct! 
// pVar is an address and so is &a
pVar = &a;

 // Correct!
// both *pVar and a are values
*pVar = a;

Advantages of Pointers

  • The pointer allows you to access any memory location.
  • It reduces the code and improves the performance.
  • It used to retrieve the strings, trees and is used with arrays, structures and functions.
  • By using pointers, you can return multiple values form function.
error: Content is protected !!