We help IT Professionals succeed at work.

pointer in C++

rwniceing
rwniceing asked
on
310 Views
Last Modified: 2014-08-27
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
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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 ?
CERTIFIED EXPERT

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

Author

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 ?
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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.

Author

Commented:
steve, thanks for your reply, reported it in my previous post

Author

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..
CERTIFIED EXPERT

Commented:
Yes, exactly.

Author

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

Commented:
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.

Author

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

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.