Avatar of rwniceing
rwniceing
 asked on

pointer in C++

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;
}

Open in new window

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
C++C

Avatar of undefined
Last Comment
Zoppo

8/22/2022 - Mon
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rwniceing

ASKER
Sorry typing mistake , it should be swap(&a, &b);
#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;
}

Open in new window


ozo, you know what I'm asking ?
Zoppo

Hi rwniceing,

since (as ozo wrote) this is not possible I'd like to ask why do you want to do this? Could you explain what you want to achieve?

ZOPPO
rwniceing

ASKER
Sorry again, it is swap(&x, &y);

I know swap is successful to swap their value,
I just think could I do it  in other way to complete the same function

Now it is swapping to their value. Could I swap their address instead of value to acheive the same  thing ?

ozo, probably, yes, the address is static at the beginning at &x  and &y when iniitializing, how about other experts' comment ?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Zoppo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
stevejacob68

Hi,

There is a error in calling of swap function in main (),

You need to specify the address of variable instead of its value.

change swap(x,y); line
with swap(&x,&y);

In swap(&x,&y), address of X and Y is passed instead of its value.
rwniceing

ASKER
steve, thanks for your reply, reported it in my previous post
rwniceing

ASKER
in other words,

int x=42 which is initialized by the computer complier and assign its address  and users could
NOT be able to assign it , Right ? for exmple,

&x=BBCC00;// that is not working..
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zoppo

Yes, exactly.
rwniceing

ASKER
Thanks for your reply. If possible, could I know what is that pointer declaration  meaning of  
int *ptr = (int *) 0x1234; ?

 what is (int*) stand for that always seen in tutorial articles on internet?

Rwniceing
Zoppo

Well, you know pointers are nothing else than some kind of integer values holding the address of a variable of a given type. If an address is assigned to a pointer (i.o.w. changing the value of the pointer) the pointer type needs to be the same, so you can't i.e. simply assign a pointer to float to a pointer to int.

In your sample the new value 0x1234 is of type int, so it needs to be type-casted to a pointer to int because the compiler doesn't allows assinging anything else than a pointer.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
rwniceing

ASKER
thanks for your, Zoppo, I will digest it with cout<< <<endl testing with that.
My goal of this topic is knowing more actual concept for pointer and basic C++ with its syntax
Zoppo

If you don't know yet I would suggest to work through this tutorial: http://www.cplusplus.com/doc/tutorial/pointers/

It includes nearly everything one needs to know about pointer including pointer-to-pointer and pointer-to-function concepts.