Pointer Comparison with
Reference Parameters and Arrays
Pointers and Reference Parameters
Have you been wondering where reference parameters got their name? It's because the formal parameter refers to the actual parameter, rather than being a copy. Now that we know that variables which refer to other variables are called pointers, it should not be surprising that reference parameters are actually pointers.
Since reference parameters are pointers, ordinarily we would use them just like pointers (and we did in the old C language); however, reference paramters are *so* common, that the creators of C++ changed the syntax for these pointer variables to make them simple to work with. So the only indication that we give to make a parameter a reference (and thus a pointer) is to denote this with a '&' character in the parameter declaration.
Example: See below or for full program see pointer_as_parms.cpp
Using reference parameters
// Example function using ref parms
void swap(int &item1, int &item2) {
int temp;
temp = item1;
item1 = item2;
item2 = temp;
}
int main() {
int a = 5, b = 10;
swap(a, b);
return 0;
}
Using pointers to mimic reference parameters
// Example function using pointers to mimic ref parms
void swap(int *item1, int *item2) {
int temp;
temp = *item1;
*item1 = *item2;
*item2 = temp;
}
int main() {
int a = 5, b = 10;
swap(&a, &b);
return 0;
}