Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 07:56 AM |
Hi,
I have a question about some questions about references to pointers. First of all, they are used to change the address (not the value) of the pointer.
1. Why can't you just pass the address of the pointer and change the address of that pointer in the receiving function?
2. Can you do a pointer to a reference instead, or is there something logically wrong with that? |
|
|
| Report Abuse |
|
|
|
| 01 Apr 2012 08:01 AM |
As far as i know references are just aliases to variables like
int A;
int& B=A;
would be the same variable... With pointers, a function reads the variable thru the pointer, with nothing the function reads a clone of the variable, with a reference it reads the variable directly. (but i dont think thats always possible so can references sometimes be pointers? o,e)
so if you have a pointer to a reference its black magic and you should leave it alone.
or wait until stravant wakes up. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 08:09 AM |
| Yeah, on LearnCpp, it says references are pretty much simplified pointers. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|
|
| 01 Apr 2012 10:31 AM |
Do you mean when you do
int lol;
int* A=&lol;
to set the adress of a pointer or wat |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 10:39 AM |
no i talk about function parameters/arguments in the first question. lolz
|
|
|
| Report Abuse |
|
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 11:25 AM |
lol go to this site and look at the examples:
htt_p://w_ww.learncpp.com/cpp-tut_orial/74-passing-arguments-by-address/ |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 12:22 PM |
| I think myrkos is still sleeping. He went to logged off of ROBLOX at 2 AM. :o |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 01 Apr 2012 12:56 PM |
I logged off at 12 midnight here. :p
So...
1. If you pass an address of a pointer itself, like:
int * a; pie(&a);
then you're passing a pointer to a pointer. But if you mean passing the value of a pointer, which is the address of another variable, you can do that, too. And a reference to a pointer works the same way.
2. You can't do a pointer to a reference just because a reference is so loosely defined by the standard, it may not even exist in memory. Not to say a reference is like a constant pointer so you wouldn't be able to modify that reference using the pointer to it. For this reason you can't even have arrays of references. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 01:08 PM |
Thanks for the response.
1. Yeah, so according to LearnCpp, you can't pass the address to a function directly. It says it's because you're technically passing it by value, so you cannot change the address because the value gets destroyed at the end of the function. Is that why we need to have a reference to a pointer to change the address? Can we have a pointer to a pointer to achieve the same thing?
2. So it's like trying to use a toy car to hold up a real car, where a reference is the toy and a pointer is the real car? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 01 Apr 2012 01:17 PM |
1. You can change the address, but it will only change inside your function. The outside variable isn't affected. And you can have either to change the address.
2. You can think of it like that. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 01:23 PM |
| But if I want it to change the original argument's address, I would have to have the reference to a pointer's address? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 01 Apr 2012 01:26 PM |
| You wouldn't actually change its address, because that's something you can't do. The addresses of variables stay the same as long as they exist. But you can change a pointer's value, i.e. where it points to, by passing a reference (or pointer) to it. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 01:30 PM |
| If you enter a pointer into the argument of the function and then change the pointer's access because your parameter is a reference to a pointer, then that would make sense right? |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 01 Apr 2012 01:32 PM |
Here's something to illustrate your first question:
int a = 10; int b = 20; void foo(int* ptr) { ptr = &b; cout << *ptr; //20 } void bar(int* &ptr) { ptr = &b; cout << *ptr; //20 } void main() { int* myptr = &a; cout << *myptr; //10 foo(myptr); cout << *myptr; //10 bar(myptr); cout << *myptr; //20 }
Both foo and bar are able to modify the pointer they recieve as an argument. The difference is, `bar` recieves the same pointer, so can modify where it points to, whereas `foo` recieves only a copy of the pointer, so changes to that pointer do not have an effect on the original.
As for question 2, a reference can essentially be read as either "These are two names for the value at the same address in memory", or "Use the actual memory address of this parameter when passed as an argument". Neither of these is a concept that can be pointed to.
|
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 01 Apr 2012 01:37 PM |
| Ahh, I understand. Thanks guys! |
|
|
| Report Abuse |
|
|