-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex6_22.cpp
47 lines (38 loc) · 870 Bytes
/
ex6_22.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! @Alan
//!
//! Exercise 6.22:
//! Write a function to swap two int pointers.
//!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//!
//! @brief swap_ptr
//! @note a pointer is an object, so it can be referenced to using &.
//! int* &_p1 means _p1 is a reference to an int pointer.
//!
void swap_ptr(int* &_p1, int* &_p2);
int main()
{
int a, b;
int *p1=&a, *p2=&b;
cout<<"Plz enter:\n";
while(cin>>a>>b)
{
p1=&a, p2=&b; //make sure p1-->a and p2-->b, otherwise funny things will happen
//and look like the swap_ptr doesn't work.
swap_ptr(p1, p2);
cout<<*p1
<<" "
<<*p2
<<"\n";
}
return 0;
}
void swap_ptr(int* &_p1, int* &_p2)
{
int *temp = _p1;
_p1 = _p2;
_p2 = temp;
}