Loading...

Passing Parameters to Functions via Pointers

Call by pointer: modifying values, simulating out params, and performance trade-offs.

In C++, when passing parameters to functions you typically use pass by value or pass by reference. However, sometimes a function needs to work directly with a memory address — and in that case, passing parameters by pointer comes into play. This method allows variables to be modified directly in memory without being copied.


1. Difference Between Passing by Value and by Pointer

When parameters are passed by value, a copy of the original variable is made. Any changes inside the function do not affect the original variable. When passed by pointer, the function can access the original memory directly.


#include <iostream>
using namespace std;

void ChangeByValue(int x) {
    x = 20;
}

void ChangeByPointer(int *p) {
    *p = 20;
}

int main() {
    int number = 10;

    ChangeByValue(number);
    cout << "After call by value: " << number << endl; // 10

    ChangeByPointer(&number);
    cout << "After call by pointer: " << number << endl; // 20
}

Conclusion: Passing parameters by pointer can change the variable’s value directly via its address.


2. Declaring Pointer Parameters

When declaring a function that takes a pointer parameter, the parameter type is written with *. When calling the function, the variable’s address is passed using &.


void Modify(int *ptr) {
    *ptr = *ptr + 5;
}

Inside the function, the expression *ptr changes the actual value of the variable being pointed to. This method saves memory and can also improve performance.


3. Multiple Pointer Parameters

You can send multiple variable addresses to a function at the same time. This is especially useful for arithmetic operations or for swapping values.


#include <iostream>
using namespace std;

void SwapValues(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 10;
    SwapValues(&x, &y);
    cout << "x: " << x << ", y: " << y << endl; // x:10, y:5
}

In this example, thanks to pointer parameters, the function accessed the variables’ memory addresses directly. The values of the variables outside the function were successfully changed.


4. const Pointer Parameters

If a function should not modify the value passed to it, the pointer parameter can be declared as const.


void Print(const int *p) {
    cout << "Value: " << *p << endl;
}

int main() {
    int n = 42;
    Print(&n);
}

In this case the function can only read the data, not change it. This increases code safety and prevents accidental data modification.


5. Pointers and Arrays (Array Parameters)

In C++, arrays actually behave like pointers. So when arrays are passed to functions, the address of the first element is passed.


void PrintArray(int *arr, int length) {
    for (int i = 0; i < length; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    PrintArray(numbers, 5);
}

Inside the function you can access array elements with arr[i] or *(arr + i). That’s because the array name actually represents the address of its first element.


6. Pointer Parameters with Dynamic Memory

Pointer parameters can also be used together with data that is created dynamically on the heap.


#include <iostream>
using namespace std;

void Fill(int *p, int length) {
    for (int i = 0; i < length; i++)
        p[i] = (i + 1) * 10;
}

int main() {
    int *arr = new int[5]; // dynamic array on the heap
    Fill(arr, 5);

    for (int i = 0; i < 5; i++)
        cout << arr[i] << " ";

    delete[] arr; // free memory
}

In this example, the pointer parameter was used to fill elements of an array allocated on the heap. After the function call, the memory must be freed with delete[].


7. Pointer to Pointer (Double Pointer)

You can also store the address of a pointer itself. In that case, a pointer to pointer (double pointer) is used. This is useful when a function needs to change the pointer itself.


void ChangePointer(int **p) {
    static int value = 99;
    *p = &value; // change the address the pointer points to
}

int main() {
    int x = 10;
    int *ptr = &x;

    ChangePointer(&ptr);
    cout << "New value: " << *ptr << endl; // 99
}

This allows the function to modify not just the data, but the pointer itself. It is commonly used in functions that manage dynamic memory.


8. Modern C++ Alternative: References and Smart Pointers

With modern C++ (C++11 and later), classic pointer parameters are often replaced by references or smart pointers:

However, in low-level systems programming, embedded systems, and performance-critical code, classic pointer parameters are still widely used.


9. TL;DR

  • int *p → defines a pointer parameter; call the function with &var.
  • Pointer parameters let functions access and modify memory directly.
  • const int *p → read-only access.
  • Arrays behave like pointers and are passed as addresses to functions.
  • int **p → pointer to pointer; the function can change the pointer itself.
  • In modern C++, reference and smart pointer are often safer.
  • All examples can be run with Visual Studio 2022 or GCC 11+.

Related Articles