Home

Passing a Pointer as Argument

 

Introduction

As we have seen so far, a function can use one or more arguments in order to carry its assignment. When necessary, a function also declares its own variable(s) to get the desired return value. Here is an example:

using namespace System;

double CalculateNetPrice(double Disc);

int main()
{
    double FinalPrice;
    double Discount = 20;

    FinalPrice = CalculateNetPrice(Discount);

    Console::WriteLine("After applying a 20% discount");
    Console::WriteLine("Final Price = {0:C}\n", FinalPrice);
    
    return 0;
}

double CalculateNetPrice(double Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * Discount / 100);
}

Here is an example of running the program:

Enter the original price: 125.95
After applying a 20% discount
Final Price = $100.76

Press any key to continue . . .

Like other variables, a pointer can be passed to a function. When declaring  and when implementing a function that takes a pointer as an argument, use the asterisk for the argument. Here is an example:

using namespace System;

double CalculateNetPrice(double *);

int main()
{  
    return 0;
}

double CalculateNetPrice(double *Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * *Discount / 100);
}

When calling the function, use the reference(s) to the variable(s). The function will perform its assignment on the referenced variable(s). After the function has performed its assignment, the changed value(s) of the argument(s) will be preserved and given to the calling function. Here is an example:

int main()
{
    double FinalPrice;
    double Discount = 20.00;

    FinalPrice = CalculateNetPrice(&Discount);

    Console::WriteLine("After applying a 20% discount");
    Console::WriteLine("Final Price = {0:C}\n", FinalPrice);
    
    return 0;
}

Here is an example of running the program:

Enter the original price: 250.50
After applying a 20% discount
Final Price = $200.40

Press any key to continue . . .  

Like a reference (native or tracking), when passing a pointer as argument to a function, the function that is receiving the argument is in fact accessing the argument's address. Therefore, like a reference, the called function has the ability to alter the value held by the pointer. The effect is the same as for the reference: if the called function modifies the value of the pointer, that value is permanently changed. This is a feature you can use to have one (even void) function returning more than one value.

Constant Pointers as Arguments

The previous section demonstrates to us that, when passing a pointer as argument, the effect is the same as passing an argument as reference. This shows that, passing a pointer as argument gives the called function direct access to the address of the variable. Besides permanently changing the value of the argument, this process also speeds up code execution. Although there are various good reasons to pass pointers as arguments, sometimes you may not want the called function to modify the value held by the variable. In fact, you can prevent this.

If a function that receives a pointer as argument is not supposed to modify the value of the argument, you can pass the argument as a constant pointer. To do this, type the const keyword on the left side of the data type of the pointer argument. Here is an example:

using namespace System;

double CalculateNetPrice(const double *Disc);

int main()
{
    double FinalPrice;
    double Discount = 20.00;

    FinalPrice = CalculateNetPrice(&Discount);

    Console::WriteLine("After applying a 20% discount");
    Console::WriteLine("Final Price = {0:C}\n", FinalPrice);
    
    return 0;
}

double CalculateNetPrice(const double *Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * *Discount / 100);
}

Passing a Pointer to a Pointer

A regular use of the double pointer is to pass it as argument. This can easily be done as follows:

void ShowValue(int **value)
{
}

A double-pointer is primarily a pointer. It is just a pointer that points to another pointer. If it's passed as argument, you can display its value using Write() or WriteLine() as done above. Here is an example:

void ShowValue(int **value)
{
	Console::WriteLine("Value = {0}", **value);
}

When calling a function that takes a double-pointer, if the variable passed was declared as a (single) pointer, type an ampersand to the left of the argument. Here is an example:

using namespace System;

void ShowValue(int **value)
{
    Console::WriteLine("Value = {0}", **value);
}

int main()
{
    int *vl = new int(428);

    ShowValue(&vl);
    
    return 0;
}

This would produce:

Value = 428
Press any key to continue...

Passing an Argument as a Reference to a Pointer

Another technique you can use it to pass an argument as a reference to a pointer. You start by passing the argument as a pointer but, to indicate that it in fact is a reference, you precede the name of the parameter with the & operator. Here is an example:

double CalculateNetPrice(double *&dicount);

When defining the function, use the parameter as a pointer. Here is an example:

double CalculateNetPrice(double *&Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * *Discount / 100);
}

When calling the function, the argument must be passed as a pointer and not as a regular variable. Here is an example:

using namespace System;

double CalculateNetPrice(double *&);

int main()
{  
    double FinalPrice = 0.00;
    double *Discount  = new double(20.00);

    FinalPrice = CalculateNetPrice(Discount);

    Console::WriteLine("After applying a 20% discount");
    Console::WriteLine("Final Price = {0:C}\n", FinalPrice);
    return 0;
}

double CalculateNetPrice(double *&Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * *Discount / 100);
}

Once again, keep in mind that, when a function receives a either pointer, a reference (native or tracking), or a reference to a pointer as argument, if that function modifies the value of the argument, when the function exits, the argument would be returned with the new value. You can use this characteristic to return many values from a single function.

Passing an Argument as a Handle

Instead of passing an argument as pointer, you can pass it as a handle. To do this, in the parentheses of the function, precede the argument's name with a cap operator. Here is an example:

double CalculateNetPrice(double ^Discount)
{
}

In the body of the function, process the argument as if it were a pointer. For example, to access its value, precede it with an asterisk. Here is an example:

double CalculateNetPrice(double ^Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * *Discount / 100);
}

As mentioned for the other arguments, if you are only declaring the function without defining it, in its parentheses, only the data type and the ^ operator are necessary. The name of the argument is important only when implementing the function. Here is an example:

using namespace System;

double CalculateNetPrice(double ^);

int main()
{  
    double FinalPrice;
    double Discount = 20.00;

    FinalPrice = CalculateNetPrice(Discount);

    Console::WriteLine("After applying a 20% discount");
    Console::WriteLine("Final Price = {0:C}\n", FinalPrice);
    return 0;
}

double CalculateNetPrice(double ^Discount)
{
    double OrigPrice;

    Console::Write("Enter the original price: ");
    OrigPrice = double::Parse(Console::ReadLine());

    return OrigPrice - (OrigPrice * Discount / 100);
}

 

 

Previous Copyright © 2006 FunctionX, Inc. Next