Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding C++ Pointers: A Practical Guide

C++ Pointers

This tutorial explains C++ pointers, their mechanics, and practical examples to help you master this essential language feature.

In C++, a pointer is a variable that holds the memory address of another variable. Pointers enable direct memory manipulation, dynamic allocation, and efficient data handling.

Address in C++

Given a variable var, the expression &var returns its address in memory. For example:

Example 1: Printing Variable Addresses in C++

#include <iostream>
using namespace std;

int main()
{
    // declare variables
    int var1 = 3;
    int var2 = 24;
    int var3 = 17;

    // print address of var1
    cout << "Address of var1: " << &var1 << endl;

    // print address of var2
    cout << "Address of var2: " << &var2 << endl;

    // print address of var3
    cout << "Address of var3: " << &var3 << endl;
}

Output

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

Addresses are displayed in hexadecimal format. The difference of 4 bytes between consecutive addresses reflects the 4‑byte size of an int on a 64‑bit system.

Note: The exact addresses may vary each time you run the program.


C++ Pointers

As shown, pointers store addresses rather than direct values. Declaring a pointer is straightforward:

int *pointVar;

Here, pointVar is a pointer to an int. The preferred syntax places the asterisk next to the type:

int* pointVar; // preferred syntax

Multiple declarations are also possible:

int* pointVar, p;

In this line, pointVar is a pointer, while p is a regular int. The asterisk associates with the variable name, not the type.

Tip: The * operator is used to declare pointers, not to perform multiplication.


Assigning Addresses to Pointers

To bind a pointer to a variable’s address, use the address‑of operator &:

int* pointVar, var;
var = 5;
// assign address of var to pointVar pointer
pointVar = &var;

Now pointVar holds the memory address of var.


Getting the Value from the Address Using Pointers

The dereference operator * retrieves the value stored at the address a pointer holds:

int* pointVar, var;
var = 5;
pointVar = &var;
cout << *pointVar << endl;   // Output: 5

Here, *pointVar yields the value of var (5). The operator is called the dereference operator.

Note: In C++, pointVar (the pointer) and *pointVar (the value it points to) are distinct entities.


Example 2: Working of C++ Pointers

#include <iostream>
using namespace std;
int main() {
    int var = 5;
    // declare pointer variable
    int* pointVar;
    // store address of var
    pointVar = &var;
    // print value of var
    cout << "var = " << var << endl;
    // print address of var
    cout << "Address of var (&var) = " << &var << endl << endl;
    // print pointer pointVar
    cout << "pointVar = " << pointVar << endl;
    // print the content of the address pointVar points to
    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

Changing the Value Pointed by Pointers

If pointVar points to var, modifying *pointVar changes the original variable:

int var = 5;
int* pointVar;
pointVar = &var;
*pointVar = 1;
cout << var << endl; // Output: 1

Both pointVar and &var hold the same address; altering the dereferenced value updates var.


Example 3: Changing Value Pointed by Pointers

#include <iostream>
using namespace std;
int main() {
    int var = 5;
    int* pointVar;
    // store address of var
    pointVar = &var;
    // print var
    cout << "var = " << var << endl;
    // print *pointVar
    cout << "*pointVar = " << *pointVar << endl << endl;
    cout << "Changing value of var to 7:" << endl;
    // change value of var to 7
    var = 7;
    // print var
    cout << "var = " << var << endl;
    // print *pointVar
    cout << "*pointVar = " << *pointVar << endl << endl;
    cout << "Changing value of *pointVar to 16:" << endl;
    // change value of var to 16
    *pointVar = 16;
    // print var
    cout << "var = " << var << endl;
    // print *pointVar
    cout << "*pointVar = " << *pointVar << endl;
    return 0;
}

Output

var = 5
*pointVar = 5

Changing value of var to 7:
var = 7
*pointVar = 7

Changing value of *pointVar to 16:
var = 16
*pointVar = 16

Common Mistakes When Working With Pointers

Below are typical errors and the correct way to handle pointers:

int var, *varPoint;

// Wrong! varPoint is an address but var is not
varPoint = var;

// Wrong!
// &var is an address
// *varPoint is the value stored in &var
*varPoint = &var;

// Correct!
// varPoint is an address and so is &var
varPoint = &var;

// Correct!
// both *varPoint and var are values
*varPoint = var;

Recommended Readings:

Understanding C++ Pointers: A Practical Guide

C Language

  1. Understanding C++ Type Conversion: Implicit, Explicit, and Casting Techniques
  2. Mastering C++ Operators: A Complete Guide with Practical Examples
  3. C++ Comments: Best Practices for Readable, Maintainable Code
  4. Mastering the C++ break Statement
  5. Mastering C++ Functions: From Basics to Advanced Usage
  6. Understanding C++ Pointers: A Practical Guide
  7. C++ Pointers and Arrays: Mastering the Relationship
  8. C++ Call by Reference: Using Pointers – Practical Examples
  9. Mastering C Pointers: A Practical, Expert‑Guided Tutorial
  10. Mastering C++ Pointers: Concepts, Examples & Practical Applications