troubleshooting Question

pointer in C++

Avatar of rwniceing
rwniceing asked on
C++C
13 Comments2 Solutions317 ViewsLast Modified:
Dear Experts,
When we read the tutorial for pointer in C++ on internet articles, most likey, swap example will be used,
for example,

#include <iostream>
using namespace std;
void swap(int *a, int *b){
 int temp = *a;
  *a = *b;
  *b = temp;
}
int main() {
int x = 42;
int y = 17;

swap(x, y);
cout <<x << '\n';//17
cout <<y << '\n';//42
return 0;
}
Probably it use swap the pointer to address's vaue to achieve the swapping.In other words, swap thier value. I want to know could we do it in other way, swap the pointer to address instead of its value ?

For example , before swap
&x //address of x =FFAA00 and its value=42
&y /address of y =BBCC00a and its value=17

after swap
&x //address of x =BBCC00 and its new value=17
&y /address of y =FFAA00 and its new value=42

Or I need to think about to do that using pointer of pointer

Please advise
Rwniceing
ASKER CERTIFIED SOLUTION
Zoppo

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros