What Does *& Mean in a Function Parameter

What does *& mean in a function parameter

It's a reference to a pointer to an int. This means the function in question can modify the pointer as well as the int itself.

You can just pass a pointer in, the one complication being that the pointer needs to be an l-value, not just an r-value, so for example

int myint;
function(&myint);

alone isn't sufficient and neither would 0/NULL be allowable, Where as:

int myint;
int *myintptr = &myint;
function(myintptr);

would be acceptable. When the function returns it's quite possible that myintptr would no longer point to what it was initially pointing to.

int *myintptr = NULL;
function(myintptr);

might also make sense if the function was expecting to allocate the memory when given a NULL pointer. Check the documentation provided with the function (or read the source!) to see how the pointer is expected to be used.

Difference between function arguments declared with & and * in C++

f2 is taking it's arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you're passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.

Passing by const reference (const double& ref) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.

Pointers are mostly used when you need to be able to pass NULL to your parameters, obviously you'd need to check then inside your function if the pointer was not NULL before using it.

What do & and * in front parameter mean?

In the first example (*) you are passing a pointer to your function and in the second example (&) you are passing a reference.

To know the difference between both, read this post : What are the differences between a pointer variable and a reference variable in C++?

Bonus question :

One of the major disadvantages of pass by value is that all arguments passed by value are copied to the parameters. When the arguments are large structs or classes, this can take a lot of time. References provide a way to avoid this penalty. When an argument is passed by reference, a reference is created to the actual argument (which takes minimal time) and no copying of values takes place. This allows us to pass large structs and classes with a minimum performance penalty.

However, this also opens us up to potential trouble. References allow the function to change the value of the argument, which in many cases is undesirable. If we know that a function should not change the value of an argument, but don’t want to pass by value, the best solution is to pass by const reference.

You already know that a const reference is a reference that does not allow the variable being referenced to be changed. Consequently, if we use a const reference as a parameter, we guarantee to the caller that the function will not (and can not) change the argument!

What does `*&` in a function declaration mean?

The & symbol in a C++ variable declaration means it's a reference.

It happens to be a reference to a pointer, which explains the semantics you're seeing; the called function can change the pointer in the calling context, since it has a reference to it.

So, to reiterate, the "operative symbol" here is not *&, that combination in itself doesn't mean a whole lot. The * is part of the type myStruct *, i.e. "pointer to myStruct", and the & makes it a reference, so you'd read it as "out is a reference to a pointer to myStruct".

The original programmer could have helped, in my opinion, by writing it as:

void myFunc(myStruct * &out)

or even (not my personal style, but of course still valid):

void myFunc(myStruct* &out)

Of course, there are many other opinions about style. :)

meaning of &variable (passed to function)

func being some arbitrary user defined function

It couldn't be "arbitrary" - it must take a pointer to int or a void* in order for the call to be legal.

This ampersand is the "take address" operator. It passes func the address of a, so that the func could, for example, modify it:

void func(int *pa) {
*pa = 4; // Note the asterisk - it "undoes" the effect of the ampersand
}

If your main prints a after the call to func, it prints 4 instead of 3.

Note that if you pass a instead of a pointer to a to a function that takes an int, not an int*, then modifications done to that int inside the function will have no effect on the parameter that you pass, because in C parameters are passed by value.

the variable a in the actual code is probably global or extern or something

It is probably not global, because there is no point in passing globals around: by virtue of being global, they are already accessible from everywhere.

Meaning of *& and **& in C++

That is taking the parameter by reference. So in the first case you are taking a pointer parameter by reference so whatever modification you do to the value of the pointer is reflected outside the function. Second is the simlilar to first one with the only difference being that it is a double pointer. See this example:

void pass_by_value(int* p)
{
//Allocate memory for int and store the address in p
p = new int;
}

void pass_by_reference(int*& p)
{
p = new int;
}

int main()
{
int* p1 = NULL;
int* p2 = NULL;

pass_by_value(p1); //p1 will still be NULL after this call
pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory

return 0;
}

Use of the & operator in C++ function signatures

A reference is not a pointer, they're different although they serve similar purpose.
You can think of a reference as an alias to another variable, i.e. the second variable having the same address. It doesn't contain address itself, it just references the same portion of memory as the variable it's initialized from.

So

string s = "Hello, wordl";
string* p = &s; // Here you get an address of s
string& r = s; // Here, r is a reference to s

s = "Hello, world"; // corrected
assert( s == *p ); // this should be familiar to you, dereferencing a pointer
assert( s == r ); // this will always be true, they are twins, or the same thing rather

string copy1 = *p; // this is to make a copy using a pointer
string copy = r; // this is what you saw, hope now you understand it better.

What does '&' do in a C++ declaration?

The "&" denotes a reference instead of a pointer to an object (In your case a constant reference).

The advantage of having a function such as

foo(string const& myname) 

over

foo(string const* myname)

is that in the former case you are guaranteed that myname is non-null, since C++ does not allow NULL references. Since you are passing by reference, the object is not copied, just like if you were passing a pointer.

Your second example:

const string &GetMethodName() { ... }

Would allow you to return a constant reference to, for example, a member variable. This is useful if you do not wish a copy to be returned, and again be guaranteed that the value returned is non-null. As an example, the following allows you direct, read-only access:

class A
{
public:
int bar() const {return someValue;}
//Big, expensive to copy class
}

class B
{
public:
A const& getA() { return mA;}
private:
A mA;
}
void someFunction()
{
B b = B();
//Access A, ability to call const functions on A
//No need to check for null, since reference is guaranteed to be valid.
int value = b.getA().bar();
}

You have to of course be careful to not return invalid references.
Compilers will happily compile the following (depending on your warning level and how you treat warnings)

int const& foo() 
{
int a;

//This is very bad, returning reference to something on the stack. This will
//crash at runtime.
return a;
}

Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.

What does & in C++ function arguments list mean?

The reason Stroustrup gives for introducing references to C++ is operator overloading.

http://www.stroustrup.com/bs_faq2.html#pointers-and-references

In your example function bar, it is no big deal whether the user has to call it as bar(&x) (because it takes a pointer), or can call it with bar(x) (because it takes a reference). At least, C programmers think it isn't.

However, when operator overloading was added to C++, Stroustrup considered that using overloaded operators with pointers is very inelegant ("ugly" in his words).

References have some advantages in functionality over pointers, such as the fact that a temporary object can be bound to a const reference, but you can't apply the & operator to it. So a pass-by-const-reference function sometimes saves the caller a line of code (to create a variable) compared with its pass-by-pointer-to-const equivalent.

For this reason, one possible convention is to accept a pointer when your function plans to store the address somewhere for future use after it returns, and a reference when it doesn't. It doesn't prevent all possible ways of creating a dangling pointer/reference, but it catches a big one. It does have unfortunate consequences when writing functional-style code, though, so it's not for everyone.

I also noticed if I use the 2nd method, I need to use -> as opposed to
. if I pass in a struct... why?

It's just the syntax inherited from C. . to access a member of a struct, -> to access a member via a pointer-to-struct. In C you can only use -> with a pointer on the LHS, and you can never use . with a pointer on the LHS. So there's no strict need for different symbols, it just helps make code more readable to have reminders. For example, if the same symbol . was used for both then (*ptr).member would mean the same thing as ptr.member, which would probably be confusing.

In C++ the difference becomes useful to the language. You can overload operator-> for a class type, for example smart pointers do. But class types can have members accessed with .. So some_smart_ptr->get(); means "call the get() function on the referand of the smart pointer", whereas some_smart_ptr.get() means "call the get() function on the smart pointer".



Related Topics



Leave a reply



Submit