| | |
passing values by reference
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
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
void CharNode::headInsert(CharNode* &head, char *d)
{
head = new CharNode(d, head);
}
If this is not enough I can download the full program
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
•
•
•
•
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.
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).
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)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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.
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......")
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)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
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?
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."
•
•
•
•
What I still do not understand is: when you pass a pointer is it not like passing by reference?
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."
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."
![]() |
Similar Threads
- Passing Structures by Reference (C++)
- Passing values between functions (C++)
- Switch menu help (C)
- C++ functions and loop errors (C++)
- Trouble w/Passing Values (C++)
- How to transfer values between ASP.NET pages (ASP.NET)
- Return values from a method by not using return (Java)
- Java passing values (Java)
Other Threads in the C++ Forum
- Previous Thread: Help with Classes and Doubly Linked Lists
- Next Thread: How to return map iterator to the vector
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






