0

I'm doing a searching for swapping two nodes of linked list and found out a block of code as follow:

void swapNode(call * &head, call * &first, call * &second) {
    // swap values, assuming the payload is an int:
    int tempValue = first->value;
    first->value = second->value;
    second->value = tempValue;
}

My question is what the meaning for put the ampersand after asterisk?

3
  • As mentioned below that specifies a reference. But it looks like you don't need it since you are not updating any of the parameters. Commented Oct 17, 2017 at 0:44
  • @AnonMail actually I don't use the head parameter, just swap the values in memory addresses first and second point to Commented Oct 17, 2017 at 0:54
  • Tested, no need for ampersand, thanks Commented Oct 17, 2017 at 0:57

1 Answer 1

1

Ampersand (&) here denotes reference. it is not the address operator.

This is the C++ way of being able to change the value of a passed-in pointer because C/C++ pass parameters by value. In C, you would use a double pointer to achieve the same.

Sign up to request clarification or add additional context in comments.

4 Comments

could you give me simpler examples about denote reference?
& denotes reference, not that it is a denote reference.
so the & used for both reference and address operator ?
@PhiTruong If you use int var as a parameter to a function, any changes to var are local to the function (i.e. they are not seen outside the function). If you use int & var as a parameter to a function any changes to var are seen inside and outside the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.