Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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
0
rwniceing
Asked:
rwniceing
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now