Passing Pointer Arguments in a Function Call in C++

A C++function may have pointer parameters.

You have learned two ways to pass arguments to a function in C++: pass-by-value and pass-by-reference. You can also pass pointer arguments in a function call. A pointer argu­ment can be passed by value or by reference. For example, you can define a function as follows:

void f(int* p1, int* &p2)

which is equivalently to

typedef int* intPointer;

void f(intPointer p1, intPointer& p2)

Consider invoking function f(q1, q2) with two pointers q1 and q2:

  • The pointer q1 is passed to pi by value. So *p1 and *q1 point to the same contents. If function f changes *p1 (e.g., *p1 = 20), *q1 is changed too. However, if func­tion f changes p1 (e.g., p1 = somePointerVariable), q1 is not changed.
  • The pointer q2 is passed to p2 by reference. So q2 and p2 are now aliases. They are essentially the same. If function f changes *p2 (e.g., *p2 = 20), *q2 is changed too. If function f changes p2 (e.g., p2 = somePointerVariable), q2 is changed too.

Listing 6.14, SwapByValue.cpp, demonstrated the effect of pass-by-value. Listing 6.17, SwapByReference.cpp, demonstrated the effect of pass-by-reference with reference variables. Both examples used the swap function to demonstrate the effect. We now give an example of passing pointers in Listing 11.4.

Listing 11.4 TestPointerArgument.cpp

1 #include <iostream>
2
using namespace std;
3
4
// Swap two variables using pass-by-value
5 void swap1(int n1, int n2)
6 {
7   
int temp = n1;
8    n1 = n2;
9    n2 = temp;
10 }
11
12
// Swap two variables using pass-by-reference
13 void swap2(int& n1, int& n2)
14 {
15   
int temp = n1;
16    n1 = n2;
17    n2 = temp;
18 }
19
20
// Pass two pointers by value
21 void swap3(int* p1, int* p2)
22 {
23   
int temp = *p1;
24    *p1 = *p2;
25    *p2 = temp;
26 }
27
28
// Pass two pointers by reference
29 void swap4(int* &p1, int* &p2)
30 {
31   
int* temp = p1;
32    p1 = p2;
33    p2 = temp;
34 }
35
36
int main()

37 {
38   
// Declare and initialize variables
39    int num1 = 1;
40   
int num2 = 2;
41
42    cout <<
“Before invoking the swap function, num1 is ”
43       << num1 << ” and num2 is ” << num2 << endl;
44
45   
// Invoke the swap function to attempt to swap two variables
46    swap1(num1, num2);
47
48    cout <<
“After invoking the swap function, num1 is ” << num1 <<
49     
” and num2 is ” << num2 << endl;
50
51    cout <<
“Before invoking the swap function, num1 is ”
52      << num1 << ” and num2 is ” << num2 << endl;
53
54   
// Invoke the swap function to attempt to swap two variables
55     swap2(num1, num2);
56
57     cout <<
“After invoking the swap function, num1 is ” << num1 <<
58       
” and num2 is ” << num2 << endl;
59
60     cout <<
“Before invoking the swap function, num1 is ”
61       << num1 << ” and num2 is ” << num2 << endl;
62
63   
// Invoke the swap function to attempt to swap two variables
64    swap3(&num1, &num2);
65
66    cout <<
“After invoking the swap function, num1 is ” << num1 <<
67     
” and num2 is ” << num2 << endl;
68
69   
int* p1 = &num1;
70   
int* p2 = &num2;
71    cout <<
“Before invoking the swap function, p1 is ”
72       << p1 << ” and p2 is ” << p2 << endl;
73
74   
// Invoke the swap function to attempt to swap two variables
75     swap4(p1, p2);
76
77    cout <<
“After invoking the swap function, p1 is ” << p1 <<
78     
” and p2 is ” << p2 << endl;
79
80   
return 0;
81 }

Four functions swap1, swap2, swap3, and swap4 are defined in lines 5-34. Function swap1 is invoked by passing the value of num1 to n1 and the value of num2 to n2 (line 46).

The swapl function swaps the values in n1 and n2. n1, numl, n2, num2 are independent vari­ables. After invoking the function, the values in variables numl and num2 are not changed.

The swap2 function has two reference parameters, int& nl and int& n2 (line 13). The references of numl and num2 are passed to nl and n2 (line 55), so nl and numl are aliases and n2 and num2 are aliases. nl and n2 are swapped in swap2. After the function returns, the values in variables numl and num2 are also swapped.

The swap3 function has two pointer parameters, pl and p2 (line 21). The addresses of numl and num2 are passed to pl and p2 (line 64), so pl and &numl refer to the same memory location and p2 and &num2 refer to the same memory location. *pl and *p2 are swapped in swap3. After the function returns, the values in variables numl and num2 are also swapped.

The swap4 function has two pointer parameters, pl and p2, passed by reference (line 29).

Invoking this function swaps pl with p2 (line 75).

An array parameter in a function can always be replaced using a pointer parameter. For array parameter or pointer example,

Recall that a C-string is an array of characters that ends with a null terminator. The size of a C-string can be detected from the C-string itself.

If a value does not change, you should declare it const to prevent it from being acciden- const parameter tally modified. Listing 11.5 gives an example.

Listing 11.5 ConstParameter.cpp

1 #include <iostream>
2
using namespace std;
3
4
void printArray(const int*, const int);
5
6
int main()
7 {
8   
int list[6] = {11, 12, 13, 14, 15, 16};
9    printArray(list,
6);
10
11   
return 0;
12 }
13
14
void printArray(const int* list, const int size)
15 {
16   
for (int i = 0; i < size; i++)
17       cout << list[i] <<
” “;
18 }

The printArray function declares an array parameter with constant data (line 4). This ensures that the contents of the array will not be changed. Note that the size parameter also is declared const. This usually is not necessary, since an int parameter is passed by value. Even though size is modified in the function, it does not affect the original size value out­side this function.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *