Commit d2b9255b authored by Tony Cheung's avatar Tony Cheung

Upload New File

parent 96d51440
#include <iostream>
using namespace std;
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
cout << "In the swap(), the value of x is: " << *x << endl;
cout << "In the swap(), the stored memory address of x is: " << x << endl;
cout << "In the swap(), the value of y is: " << *y << endl;
cout << "In the swap(), the stored memory address of y is: " << y << endl;
}
int main()
{
int a = 1;
int b = 2;
cout << "Before calling swap(), the value of a is: " << a << endl;
cout << "Before calling swap(), the memory address of a is: " << &a << endl;
cout << "Before calling swap(), the value of b is: " << b << endl;
cout << "Before calling swap(), the memory address of b is: " << &b << endl;
swap(&a, &b);
cout << "After calling swap(), the value of a is: " << a << endl;
cout << "After calling swap(), the memory address of a is: " << &a << endl;
cout << "After calling swap(), the value of b is: " << b << endl;
cout << "After calling swap(), the memory address of b is: " << &b << endl;
return 0;
}
//output
/*
Before calling swap(), the value of a is: 1
Before calling swap(), the memory address of a is: 010FF82C
Before calling swap(), the value of b is: 2
Before calling swap(), the memory address of b is: 010FF820
In the swap(), the value of x is: 2
In the swap(), the stored memory address of x is: 010FF82C
In the swap(), the value of y is: 1
In the swap(), the stored memory address of y is: 010FF820
After calling swap(), the value of a is: 2
After calling swap(), the memory address of a is: 010FF82C
After calling swap(), the value of b is: 1
After calling swap(), the memory address of b is: 010FF820
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment