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
LVL 1
rwniceingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
int x = 42;
int y = 17;
declares &x and &y as fixed positions in memory which cannot be changed
0
rwniceingAuthor Commented:
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 ?
0
ZoppoCommented:
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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rwniceingAuthor Commented:
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 ?
0
ZoppoCommented:
I still don't know what's you're goal, but here something you should keep in mind:

Every variable (at least nearly every, it's i.e. different for 'register' variables) is some kind of placeholder of an address, when the compiler creates the binary code it decides where to put a variable (local variable commonly are put on the stack, occurances use the current stack pointer + offset to the local variable).

This means the address of the variable is kind of hardcoded in the binary, there's no way to change it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevejacob68Commented:
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.
0
rwniceingAuthor Commented:
steve, thanks for your reply, reported it in my previous post
0
rwniceingAuthor Commented:
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..
0
ZoppoCommented:
Yes, exactly.
0
rwniceingAuthor Commented:
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
0
ZoppoCommented:
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.
0
rwniceingAuthor Commented:
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
0
ZoppoCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.