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

Recommended Answers

All 10 Replies

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.

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

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.

>>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......")

Yeah, siddhant is absolutely right !!

But this link might be helpful too ...

Yes, I am new to C++. Siddhant Sanyam is right when he feels I am confused about address-of operator and reference. What I still do not understand is: when you pass a pointer is it not like passing by reference? in that case, what happens in the function when executed is not lost . Also how can I tell if "&" when appearing in the parameters list is reference or "address-of" operator?

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 '&' ...

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

Read http://www.parashift.com/c++-faq-lite/references.html from top to bottom.

>>What I still do not understand is: when you pass a pointer is it not like passing by reference?
Well, there is a clear line of difference. But at the beginning, it may feel a bit fuzzy.
read the homepage of stroustrup http://www.research.att.com/~bs/bs_faq2.html#pointers-and-references to get an insight. I am in a hurry right now. If you don't get anything, you can get back to us and ask again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.