#include <stdio.h>
#include <iostream>
using namespace std;
void swap_numbers_using_pointers(int *num1, int *num2);
void swap_numbers_using_references(int &num1, int &num2);
int main()
{
int num1 = 1;
int num2 = 2;
swap_numbers_using_pointers(&num1, &num2);
cout << "num1 = " << num1 << endl << "num2 = " << num2;
cout << endl << endl;
swap_numbers_using_references(num1, num2);
cout << "num1 = " << num1 << endl << "num2 = " << num2;
getchar();
return 0;
}
void swap_numbers_using_pointers(int *num1, int *num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
void swap_numbers_using_references(int &num1, int &num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
/*
This tutorial compares the use of pointers and references.
The pointer and reference functions are taken from the two previous tutorials.
I published this extra tutorial just to let you see them together.
*/
|