Both asterisk and ampersand in a parameter
In your expression BinaryNode * & t)
BinaryNode* & t
------------- -----
BinaryNode pointer t is reference variable
so t
is reference to pointer of BinaryNode class.
Pointer of the address of t?
You are confused ampersand &
operator in c++. that give address of an variable. but syntax is different.
ampersand &
in front of some of variable like below:
BinaryNode b;
BinaryNode* ptr = &b;
But following way is for reference variable (its simple not pointer):
BinaryNode b;
BinaryNode & t = b;
and your is like below:
BinaryNode b;
BinaryNode* ptr = &b;
BinaryNode* &t = ptr;
Pointers in C: when to use the ampersand and the asterisk?
You have pointers and values:
int* p; // variable p is pointer to integer type
int i; // integer value
You turn a pointer into a value with *
:
int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to
You turn a value into a pointer with &
:
int* p2 = &i; // pointer p2 will point to the address of integer i
Edit:
In the case of arrays, they are treated very much like pointers. If you think of them as pointers, you'll be using *
to get at the values inside of them as explained above, but there is also another, more common way using the []
operator:
int a[2]; // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element
To get the second element:
int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element
So the []
indexing operator is a special form of the *
operator, and it works like this:
a[i] == *(a + i); // these two statements are the same thing
C++: difference between ampersand & and asterisk * in function/method declaration?
Both do the same, but one uses references and one uses pointers.
See my answer here for a comprehensive list of all the differences.
C++ functions: ampersand vs asterisk
Pointers (ie. the '*') should be used where the passing "NULL" is meaningful.
For example, you might use a NULL to represent that a particular object needs to be created, or that a particular action doesn't need to be taken.
Or if it ever needs to be called from non-C++ code. (eg. for use in shared libraries)
eg. The libc function time_t time (time_t *result);
If result
is not NULL, the current time will be stored. But if result
is NULL, then no action is taken.
If the function that you're writing doesn't need to use NULL as a meaningful value then using references (ie. the '&') will probably be less confusing - assuming that is the convention that your project uses.
How to use ampersands and asterisks when passing pointers by reference into functions
I believe your confusion is centered around lines B and C, so let's break it down.
Line B uses the parameter vector<GraphVertex>* G
. This means that this vector owns all of these GraphVertices - it contains the actual vertex data and is in charge of allocating and deallocating them. The fact that this is passed by pointer is just to avoid copying the vector when the function is called.
Ok, so what does that mean for Line A? The vertices don't own other vertices - so they store them by pointer. These are pointing at GraphVertices which are in the vector on line B.
For line C, begin()
and end()
return iterators, and you are correct that they do have to be dereferenced to get a reference to the actual GraphVertex struct. This dereference happens for you inside of the any_of
call, which is why the lambda takes a reference.
So in line D, vertex
is a reference to a GraphVertex
inside of G
. &vertex
just converts that reference into a pointer, nothing special.
And on line F, the &
is completely redundant in this case. This probably is from common practice of writing for (Foo& foo : fooList)
- but here it is just confusing.
Finally other answers have good suggestions as far as style.
Why is `&` (ampersand) put in front of some method parameters?
You need to take the address of error
because the function needs to modify it. error
is passed by pointer, so you need the "take address" operator &
for it.
C and Objective-C pass parameters by value. If you pass error
without an ampersand and the method that you call modifies it, your function that made the call would not see any changes, because the method would operate on its local copy of NSError*
.
You know that you need an ampersand in front of the corresponding parameter if you look at the signature of the method and see **
there:
- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error
// ^ one ^^ two
What does ampersand mean in function call?
Yes, you are right: the ampersand takes the address of an lvalue (a variable) and passes it as pointer.
Your adjust()
function would look like:
void adjust(double *a, double f) {
... do a lot of stuff
*a = *a * f/2+1.0; // dummy formula that will change the content
...
};
So in the function you'd use *a
every time you'd want to use the value pointed at by the first argument, and everytim you want to assign a new value to the original variable.
Related Topics
What's the Real Reason to Not Use the Eof Bit as Our Stream Extraction Condition
How to Cin and Cout Some Unicode Text
How to Convert from Lpctstr to Std::String
Can Void* Be Used to Store Function Pointers
Why Does C++ Parameter Scope Affect Function Lookup Within a Namespace
Convert Integer to Binary and Store It in an Integer Array of Specified Size:C++
How to Legally Reinterpret_Cast Between Layout-Compatible Standard-Layout Types
Differencebetween "::" "." and "->" in C++
What Is the Use of "Using Namespace Std"
Why Does Pointer to Int Convert to Void* But Pointer to Function Convert to Bool
How to Call a Pointer-To-Member-Function
Function Already Defined Error in C++
Portable End of Line (Newline)
When to Use C++ Private Inheritance Over Composition
Opencv on MAC Is Not Opening Usb Web Camera