Passing Pointers to Functions in C++

By
Last updated:

Before we noted that there are three ways to pass arguments to a function: by value, by reference, and by pointer. If the passing pointers to functions in C++ is intended to modify variables in the calling program, these variables cannot be passed by value, since the function obtains only a copy of the variable. However, either a reference argument or a pointer can be used in this situation.

Passing Simple Variables

We’ll first review how arguments are passed by reference, and then compare this to passing pointer arguments. The PASSREF program shows passing by reference.

// passref.cpp
// arguments passed by reference
#include <iostream>
using namespace std;
int main()
{
void centimize(double&); //prototype
double var = 10.0; //var has value of 10 inches
cout << “var = “ << var << “ inches” << endl;
centimize(var); //change var to centimeters
cout << “var = “ << var << “ centimeters” << endl;
return 0;
}
//--------------------------------------------------------------
void centimize(double& v)
{
v *= 2.54; //v is the same as var
}

Here we want to convert a variable var in main() from inches to centimeters. We pass the variable by reference to the function centimize(). (Remember that the & following the data type double in the prototype for this function indicates that the argument is passed by reference.) The centimize() function multiplies the original variable by 2.54. Notice how the function refers to the variable. It simply uses the argument name v; v and var are different names for the same thing.

Once it has converted var to centimeters, main() displays the result. Here’s the output of PASSREF:
var = 10 inches
var = 25.4 centimeters

The next example, PASSPTR, shows an equivalent situation when pointers are used:

// passptr.cpp
// arguments passed by pointer
#include <iostream>
using namespace std;
int main()
{
void centimize(double*); //prototype
double var = 10.0; //var has value of 10 inches
cout << “var = “ << var << “ inches” << endl;
centimize(&var); //change var to centimeters
cout << “var = “ << var << “ centimeters” << endl;
return 0;
}
//--------------------------------------------------------------
void centimize(double* ptrd)
{
*ptrd *= 2.54; //*ptrd is the same as var
}

The output of PASSPTR is the same as that of PASSREF.

The function centimize() is declared as taking an argument that is a pointer to double:
void centimize(double*) // argument is pointer to double
When main() calls the function, it supplies the address of the variable as the argument: centimize(&var);

Remember that this is not the variable itself, as it is in passing by reference, but the variable’s address.

Because the centimize() function is passed an address, it must use the dereference operator, *ptrd, to access the value stored at this address:

*ptrd *= 2.54; // multiply the contents of ptrd by 2.54 Of course this is the same as

*ptrd = *ptrd * 2.54; // multiply the contents of ptrd by 2.54

where the standalone asterisk means multiplication. (This operator really gets around.)

Since ptrd contains the address of var, anything done to *ptrd is actually done to var.

Passing a pointers to functions in C++ as an argument to a function is in some ways similar to passing a reference. They both permit the variable in the calling program to be modified by the function. However, the mechanism is different. A reference is an alias for the original variable, while a pointer is the address of the variable.

Passing Arrays

We’ve seen numerous examples, of arrays passed as arguments to functions, and their elements being accessed by the function. Until this chapter, since we had not yet learned about pointers, this was done using array notation. However, it’s more common to use pointer notation instead of array notation when arrays are passed to functions. The PASSARR program shows how this looks:

// passarr.cpp
// array passed by pointer
#include <iostream>
using namespace std;
const int MAX = 5; //number of array elements
int main()
{
void centimize(double*); //prototype
double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 };
centimize(varray); //change elements of varray to cm
for(int j=0; j<MAX; j++) //display new array values
cout << “varray[“ << j << “]=”
<< varray[j] << “ centimeters” << endl;
return 0;
}
//--------------------------------------------------------------
void centimize(double* ptrd)
{
for(int j=0; j<MAX; j++)
*ptrd++ *= 2.54; //ptrd points to elements of varray
}

The prototype for the function is the same as in PASSPTR; the function’s single argument is a pointer to double. In array notation this is written as void centimize(double[]);

That is, double* is equivalent here to double[], although the pointer syntax is more commonly used.

Read More Topics
Array structure and union in C
Virtual function in C++
Variables in C

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment