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

Mastering C++ Pointers: Concepts, Examples & Practical Applications

What Are Pointers?

A pointer in C++ is a variable that stores the memory address of another variable. The pointer’s type must match the type of the data it points to. For example, an int* can only hold addresses of int objects.

Pointers allow a program to manipulate data directly in memory, enabling efficient data structures, dynamic allocation, and call‑by‑reference semantics.

Understanding Memory Addresses

Every variable is allocated a unique spot in RAM. The reference operator & retrieves that address. For a variable x, &x yields its memory location.

Pointer Declaration Syntax

Declarations follow the pattern:

datatype *pointer_name;

Examples:

int *p;      // pointer to int
double *d;   // pointer to double
char *c;     // pointer to char

Reference (&) vs. Dereference (*)

The & operator gives a variable’s address; the * operator retrieves the value stored at that address.

#include <iostream>
using namespace std;

int main() {
    int x = 27;
    int *ip = &x;

    cout << "Value of x: " << x << endl;
    cout << "Address of x: " << ip << endl;
    cout << "Value via pointer: " << *ip << endl;
    return 0;
}

Output:

Value of x: 27
Address of x: 0x7ffeefbff5ac
Value via pointer: 27

Pointers and Arrays

Array names decay to pointers to their first element. Therefore:

int arr[5];
int *p = arr;   // correct
int *q = &arr;   // incorrect – &arr yields address of the entire array, not its first element

Using a pointer to iterate over an array:

#include <iostream>
using namespace std;

int main() {
    int *p;
    int arr[] = {10, 34, 13, 76, 5, 46};
    p = arr;
    for (int i = 0; i < 6; ++i) {
        cout << *p++ << endl;
    }
    return 0;
}

Output:

10
34
13
76
5
46

Null Pointers

When a pointer has no valid target, it should be initialized to NULL (or nullptr in C++11 and later). This prevents accidental dereferencing.

#include <iostream>
using namespace std;

int main() {
    int *ip = nullptr;
    cout << "ip = " << ip << endl; // prints 0
    return 0;
}

Using Pointers in Functions

Passing a pointer to a function allows the function to modify the original variable. This technique implements call‑by‑reference.

#include <iostream>
using namespace std;

void modify(int *p1, int *p2) {
    *p1 = 10;
    *p2 = 11;
}

int main() {
    int a = 5, b = 5;
    cout << "Before: a=" << a << ", b=" << b << endl;
    modify(&a, &b);
    cout << "After: a=" << a << ", b=" << b << endl;
    return 0;
}

Output:

Before: a=5, b=5
After: a=10, b=11

Benefits of Pointers

Quick Summary

C Language

  1. Understanding C++ Pointers: A Practical Guide
  2. C++ Arrays Demystified: Declaration, Initialization, and Pointer Techniques
  3. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  4. Mastering C++ Char Data Types: Declaration, ASCII, and Conversion Techniques
  5. C++ Operator Overloading – A Practical Guide with Code Examples
  6. C++ Functions Explained with Practical Code Examples
  7. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  8. Pointers in C: A Comprehensive Guide to Types, Usage, and Best Practices
  9. Function Pointers in C: Practical Examples and Best Practices
  10. Mastering C Pointers: Practical Steps to Advanced Programming