943,641 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 954
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 9th, 2009
0

passing values by reference

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shabtai is offline Offline
6 posts
since Apr 2009
Apr 9th, 2009
0

Re: passing values by reference

Click to Expand / Collapse  Quote originally posted by Shabtai ...
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
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.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Apr 9th, 2009
0

Re: passing values by 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).
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 9th, 2009
0

Re: passing values by reference

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.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Apr 9th, 2009
0

Re: passing values by reference

>>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......")
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 9th, 2009
0

Re: passing values by reference

Yeah, siddhant is absolutely right !!

But this link might be helpful too ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 9th, 2009
0

Re: passing values by reference

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shabtai is offline Offline
6 posts
since Apr 2009
Apr 9th, 2009
0

Re: passing values by reference

Click to Expand / Collapse  Quote originally posted by Shabtai ...
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 '&' ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 9th, 2009
0

Re: passing values by reference

Click to Expand / Collapse  Quote originally posted by Shabtai ...
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 ...
Last edited by tux4life; Apr 9th, 2009 at 11:36 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 9th, 2009
0

Re: passing values by reference

Maybe the following article about pointers might help you to understand it better: http://www.cplusplus.com/forum/articles/418/
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Classes and Doubly Linked Lists
Next Thread in C++ Forum Timeline: How to return map iterator to the vector





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC