passing values by reference

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 6
Reputation: Shabtai is an unknown quantity at this point 
Solved Threads: 0
Shabtai Shabtai is offline Offline
Newbie Poster

passing values by reference

 
0
  #1
Apr 9th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: passing values by reference

 
0
  #2
Apr 9th, 2009
Originally Posted by Shabtai View 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
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: passing values by reference

 
0
  #3
Apr 9th, 2009
>>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 Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: passing values by reference

 
0
  #4
Apr 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: passing values by reference

 
0
  #5
Apr 9th, 2009
>>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......")
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: passing values by reference

 
0
  #6
Apr 9th, 2009
Yeah, siddhant is absolutely right !!

But this link might be helpful too ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: Shabtai is an unknown quantity at this point 
Solved Threads: 0
Shabtai Shabtai is offline Offline
Newbie Poster

Re: passing values by reference

 
0
  #7
Apr 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: passing values by reference

 
0
  #8
Apr 9th, 2009
Originally Posted by Shabtai View Post
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 '&' ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: passing values by reference

 
0
  #9
Apr 9th, 2009
Originally Posted by Shabtai View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,971
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: passing values by reference

 
0
  #10
Apr 9th, 2009
Maybe the following article about pointers might help you to understand it better: http://www.cplusplus.com/forum/articles/418/
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 695 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC