The following is a statement from a program on linked lists given to us in class. I do not understand why I have to add & for address when what is passed is a pointer which means it is passed by reference.:
void CharNode::headInsert(CharNode* &head, char *d)
{
head = new CharNode(d, head);
}
If this is not enough I can download the full program:S
head is a pointer to a list of pointers. If you pass head only (passing by value), the changes on head won't have any effect. That is why you are passing it reference.
kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
>>head is a pointer to a list of pointers.
Wrong. head is a reference to pointer to CharNode.
To understand this(that why it is been passed by reference), just remember the basic pass by reference, i.e. pass by reference allows you to change the value of the parameter passed.
In this example. if you passed by value:
The compiler will create a copy of head(which is a pointer to CharNode) and assign some new memory location to it. Then, as soon as the function exits, the copy of head will be lost, and the newly created memory will be orphaned.
And you will not get the address of the newly created memory. The whole purpose of the funtion will be lost.
Remember, (loosely speaking) when you just need to read the value of parameter, use call by value and when you want to write a value to the parameters, use call by reference.(This remark is off-course not valid at all! But for you, at this stage, it will cause no harm in your programming. When you mastered the concept, learn something about const-correctness which will be a slightly advanced topic for you maybe).
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
Siddhant, everybody knows some variable preceded by & is a reference. What I meant is the actual parameter represented by head is a pointer to a list of pointers. That is exact in the case of a linked list.
Moreover, a reference and pointer work in the same way internally.
kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
>>everybody knows some variable preceded by & is a reference
Sorry, But ampersand(&) serves two purpose in C++. Address-of operator and reference.
>>What I meant is the actual parameter represented by head is a pointer to a list of pointers
I don't know how can you conclude this by just looking at the given code. Moreover, there is nothing as list in c++( maybe u meant arrays)[spare me if you assumed the STL lists, as STL is not part of core language, the STL lists are only user defined type]
>>Moreover, a reference and pointer work in the same way internally.
Absolutely Right.
To the OP:
Like I said, the & serves two puposes in C++. Here it is used as to tell "this variable is a referene" while some other time it can be used as a address of operator. These are two separate things and one should not confused with it.( I assume that you were confused because you said "why I have to add & for address when......")
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
Yeah, siddhant is absolutely right !!
But this link might be helpful too ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Also how can I tell if "&" when appearing in the parameters list is reference or "address-of" operator?
It depends on how you're using it, if you're using the &-operator in combination with a pointer then we're talking about the address-operator '&', if you're using it in a function declaration we're talking about the reference-operator '&' ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
What I still do not understand is: when you pass a pointer is it not like passing by reference?
Yes, they're both very equal, but there's clearly a difference between them: using a pointer you can change the memory address where the pointer points to, a reference is always pointing to the same memory address, you can't change this ...
BTW, I would like to recommend you to use references as much as you can instead of pointers (that's actually the purpose of why they invented it) ...
Dealing with references is also much simpler and less error-prone than dealing with pointers ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140