Intermediate Business Programming with C++
In a previous lecture the three methods of passing arguments to a function were discussed. One of those was passing by pointers. In order for passing by pointer to be a viable approach, the pointer argument must contain the address of a variable. What is then passed by value is the address of this variable. This method of argument passing can be very helpful especially when the variable whose address is passed occupies a large amount of memory.
When pointers are used as arguments of functions, the functional prototype and definition must contain the pointer operator as in the following example:
void swap(int*, int* );
While the pointer operator appears both in the prototype and the definition of the functions it does not appear in the call. See BYPOINTER.CPP.
Passing by a pointer is different from passing by reference in that
-
the memory to which the pointer points may be changed within the function
-
the pointer would have to be dereferenced.
-
the pointer would take up stack memory while the reference would not.
Passing by a pointer is different from passing by value because it permits:
-
input to and output of functions by arrays
-
modification of multiple variables
-
faster function execution
-
less stack memory use.
Категории