What does a reference to a pointer store?
Oct 26, 2011 at 4:31pm UTC
I'm a little confused about what a pointer to a reference is. How would you describe it. For example:
[code
int n;
int* m = &n[/code]
Here pointer m stores the memory address of an integer 'n'.
Here m stores the memory address of integer 'n'.
1 2 3
int n;
int * j = &n
int *& m = j
j is a pointer which stores the memory address of integer 'n'.
What is m in this case? What does it store?
Second, is there a difference between passing a pointer:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
int n = 5;
int * m = &n;
foo(m);
}
void foo(int * j)
{
//
}
...and passing a pointer to a reference?
1 2 3 4 5 6 7 8 9 10 11
int main()
{
int n = 5;
int * m = &n;
foo(m);
}
void foo(int *& j)
{
//
}
Thanks Guys!
Last edited on Oct 26, 2011 at 4:53pm UTC
Oct 26, 2011 at 4:37pm UTC
Conceptually, a reference "is" the variable it refers to.
Therefore
Here, m "is" n. They are just different names for the same variable.
With that in mind:
1 2 3
int n;
int * j = &n;
int *& m = j;
Here, j points to n
And m "is" j. Thererfore m also points to n, because m and j are one and the same.
EDIT:
Also
int *&
is a reference to a pointer, not a pointer to a reference. ;P
Last edited on Oct 26, 2011 at 4:46pm UTC
Oct 26, 2011 at 5:04pm UTC
EDIT:
Also int*& is a reference to a pointer, not a pointer to a reference. ;P
Thanks for making that clear ... and for your answer - but I'm still a little confused:
If m and j are one and the same, then why are there situations where one can't be used instead of another. For instance, in this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
void print(char * );
void replaceWordWith(char *&, char *);
int main()
{
char * s = "Hello" ;
print(s);
replaceWordWith(s,"Bye" );
print(s);
return 0;
}
void print(char * c)
{
cout<<c<<endl;
}
void replaceWordWith(char *& c, char * s)
{
c = s;
}
Output
I don't think a simple pointer could replace the
char *& c
here
Last edited on Oct 26, 2011 at 5:05pm UTC
Oct 26, 2011 at 5:51pm UTC
I don't think a simple pointer could replace the char*& c here
No no... pointers and references are not one and the same. The reference is one and the same with the object it refers to.
In your example....
1 2 3
void replaceWordWith(char *& c, char * s);
replaceWordWith(s,"Bye" );
Here, since replaceWordWith's 'c' parameter is a reference, c "is" whatever gets passed to it. Since you're passing it 's', this means that in replaceWordWith, c "is" s. This is why when you change c, s is changed. Because they're just different names for the same variable.
You are correct in thinking that if you replace c with a regular pointer (and not a reference to a pointer) then the same thing will not be accomplished. That is because doing so will make c a separate variable from s, and changing c will not change s.
Oct 26, 2011 at 5:59pm UTC
That's an excellent explanation!
Thanks, really appreciate your help.
I think I've got it now :)
Topic archived. No new replies allowed.