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

C++ Call by Reference: Using Pointers – Practical Examples

C++ Call by Reference: Using Pointers – Practical Examples

This guide explains how to pass arguments by reference in C++ using pointers, with clear code examples that demonstrate swapping values and more.

In the world of C++ functions, the default way to pass data is by value: the function receives a copy of the original data. While this protects the original variable, it can be inefficient for large objects and prevents the function from modifying the caller’s data.

Passing by reference solves both problems. Instead of copying the data, the function receives a reference (or address) to the original variable, allowing direct manipulation. This is achieved in two equivalent ways: using C++ references or raw pointers. Understanding both approaches gives you flexibility and deeper insight into memory handling.

// Function that takes a value
void func1(int numVal) {
    // code
}

// Function that takes a reference (C++ reference syntax)
void func2(int &numRef) {
    // code
}

int main() {
    int num = 5;

    // Pass by value
    func1(num);

    // Pass by reference
    func2(num);

    return 0;
}

The & in void func2(int &numRef) indicates that the parameter is a reference. When func2 is called, the address of num is passed, not its value, allowing the function to modify num directly.

C++ Call by Reference: Using Pointers – Practical Examples

Example 1: Swapping Values with C++ References

#include <iostream>
using namespace std;

// Swap function using references
void swap(int &n1, int &n2) {
    int temp = n1;
    n1 = n2;
    n2 = temp;
}

int main() {
    int a = 1, b = 2;

    cout << "Before swapping\n";
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";

    swap(a, b);

    cout << "\nAfter swapping\n";
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";

    return 0;
}

Output

Before swapping
a = 1
b = 2

After swapping
a = 2
b = 1

In this example, swap receives two references. Because n1 and n2 are aliases for a and b, changes inside the function affect the original variables.


Example 2: Swapping Values with Pointers

#include <iostream>
using namespace std;

// Function prototype using pointers
void swap(int*, int*);

int main() {
    int a = 1, b = 2;

    cout << "Before swapping\n";
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";

    // Pass addresses of a and b
    swap(&a, &b);

    cout << "\nAfter swapping\n";
    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";
    return 0;
}

// Swap function using pointers
void swap(int* n1, int* n2) {
    int temp = *n1;
    *n1 = *n2;
    *n2 = temp;
}

Output

Before swapping
a = 1
b = 2

After swapping
a = 2
b = 1

Here, swap accepts raw pointers. By dereferencing with *, the function accesses and modifies the values at the provided addresses. The result is identical to the reference version, but the pointer syntax makes the address passing explicit.

Choosing between references and pointers depends on the context: references offer safer, cleaner syntax, while pointers provide explicit control over memory addresses and are necessary for dynamic allocation.

C Language

  1. Mastering C# Using Statements: Imports, Aliases, and Static Directives
  2. Understanding C++ Pointers: A Practical Guide
  3. C++ Pointers and Arrays: Mastering the Relationship
  4. Mastering C Pointers: A Practical, Expert‑Guided Tutorial
  5. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  6. 13 Malware Types in 2021: Threats, Examples, and Prevention
  7. 5 Data Center Models Explained: Design, Use Cases, and Examples
  8. Exploring 8 Key Tech Trends of 2021 with Real-World Examples
  9. Understanding C++ References: Key Concepts & Differences from Pointers
  10. Mastering Simple and Complex Data Types in C++ with PLCnext Engineer