Link to home
Create AccountLog in
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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of rwniceing
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 ?
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
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 ?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
steve, thanks for your reply, reported it in my previous post
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..
Yes, exactly.
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
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.
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
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.