Home

C++ Examples: Returning a Double-Pointer

 

As opposed to returning one, you can return a double-pointer. Primarily, you must specify this by preceding the name of the function with two asterisks. Here is an example:

double ** GetWeeklySalary()
{

}

Once again, in the body of the function, do whatever you want, applying some of the techniques we have studied so far about functions and pointers. Before closing the function, remember to return a double-pointer of the appropriate type. Here is an example:

double ** GetWeeklySalary()
{
	double w      = 880.24;
	double *ws    = &w;
	double **wwss = &ws;

	return wwss;
}

When calling the function, if you want to access the value it returns, you must precede it with a double asterisk. Here is an example:

//---------------------------------------------------------------------------
#include <iostream>
using namespace std;

double ** GetWeeklySalary()
{
	double w      = 880.24;
	double *ws    = &w;
	double **wwss = &ws;

	return wwss;
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
    double WeeklySalary = **GetWeeklySalary();

    cout << "Weekly Salary: " << WeeklySalary << endl;

    return 0;
}
//---------------------------------------------------------------------------

This would produce:

Weekly Salary: 880.24
 

Home Copyright © 2006-2016, FunctionX, Inc.