Passing Variables by Address


A C++ function can change the value of a non-array variable passed to it. The key to making this work is to pass the variable's address to the function instead of the variable's value. The called function can store the address passed to it in a pointer variable, and then dereference the pointer to access (and change) the value of the variable it points to.

#include <iostream>

using std::cout;
using std::endl;

void add_to_int(int*);

int main()
{
    int num = 5;

    cout << "In main(), value of num is " << num << endl;
    cout << "In main(), address of num is " << &num << endl << endl;

    add_to_int(&num);

    cout << "In main(), value of num is now " << num << endl;

    return 0;
}

void add_to_int(int* ptr)
{
    cout << "In add_to_int(), value of ptr is " << ptr << endl;
    cout << "In add_to_int(), address of ptr is " << &ptr << endl;
    cout << "In add_to_int(), value of variable pointed to by ptr is " << *ptr << endl << endl;

    *ptr += 10;

    cout << "In add_to_int(), value of variable pointed to by ptr is now " << *ptr << endl << endl;
}

The output from this program when compiled and run on turing is:

In main(), value of num is 5
In main(), address of num is 0x7ffc7c7a213c

In add_to_int(), value of ptr is 0x7ffc7c7a213c
In add_to_int(), address of ptr is 0x7ffc7c7a2108
In add_to_int(), value of variable pointed to by ptr is 5

In add_to_int(), value of variable pointed to by ptr is now 15

In main(), value of num is now 15

As you can see, the value of the variable num was changed by the add_to_int() function.

Passing Pointers to Functions

Passing a pointer to a function is equivalent to passing the address of the variable to which it points. For example:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

void a_function(string*);
void another_function(string*);

int main()
{
    string str = "alpha";
    string* ptr = &str;

    cout << "In main(), value of str is " << str << endl;
    cout << "In main(), address of str is " << &str << endl << endl;

    cout << "In main(), value of ptr is " << ptr << endl;
    cout << "In main(), address of ptr is " << &ptr << endl;
    cout << "In main(), value of variable pointed to by ptr is " << *ptr << endl << endl;

    *ptr = "beta";
    
    cout << "In main(), value of str is now " << str << endl << endl;

    a_function(ptr);

    cout << "In main(), value of str is now " << str << endl;

    return 0;
}

void a_function(string* s)
{
    cout << "In a_function(), value of s is " << s << endl;
    cout << "In a_function(), address of s is " << &s << endl;
    cout << "In a_function(), value of variable pointed to by s is " << *s << endl << endl;

    *s = "gamma";

    cout << "In a_function(), value of variable pointed to by s is now " << *s << endl << endl;

    another_function(s);
    
    cout << "In a_function(), value of variable pointed to by s is now " << *s << endl << endl;
}

void another_function(string* s)
{
    cout << "In another_function(), value of s is " << s << endl;
    cout << "In another_function(), address of s is " << &s << endl;
    cout << "In another_function(), value of variable pointed to by s is " << *s << endl << endl;

    *s = "delta";

    cout << "In another_function(), value of variable pointed to by s is now " << *s << endl << endl;
}

When compiled and executed on turing

In main(), value of str is alpha
In main(), address of str is 0x7ffee5b92990

In main(), value of ptr is 0x7ffee5b92990
In main(), address of ptr is 0x7ffee5b92988
In main(), value of variable pointed to by ptr is alpha

In main(), value of str is now beta

In a_function(), value of s is 0x7ffee5b92990
In a_function(), address of s is 0x7ffee5b92958
In a_function(), value of variable pointed to by s is beta

In a_function(), value of variable pointed to by s is now gamma

In another_function(), value of s is 0x7ffee5b92990
In another_function(), address of s is 0x7ffee5b92928
In another_function(), value of variable pointed to by s is gamma

In another_function(), value of variable pointed to by s is now delta

In a_function(), value of variable pointed to by s is now delta

In main(), value of str is now delta

Returning Pointers

Member functions may also sometimes return a variable or object by address or return a pointer to a constant variable. This is most commonly done if the member function either returns an array data member or a C string or returns a pointer to a dynamically-allocated object.

Returning a pointer to a local automatic variable declared inside a function is normally a mistake. The local variable will be deallocated automatically when the function ends, and the calling code will be left with a pointer to a variable that no longer exists.