d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Is The Point Of An &?
Add Reply New Topic New Poll
Member
Posts: 54,832
Joined: Jan 10 2009
Gold: 1,992.00
May 4 2015 12:43am
int main() {
int x = 0, y = 1, z = 2;
double b[3] = {1.9, 2.3, 3.0};
larger(x + y, z);
x = middle(x, y, y, z, z);
printAll(sqrt(b[1]), rand());
swap(x, y);
cout << mystery(y, mystery(y, b[0]));


void swap (int &a, int &b)

What is the point of the ampersand before the a and b integers?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
May 4 2015 07:13am
& gets the memory address of the variable
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
May 4 2015 02:09pm
Don't mistake &variable for variable & variable though. Same goes with variable && variable. First is is for referencing the memory address at a specific variable, the second is a bitwise and operation, and the last is a logical and operation.
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
May 8 2015 11:02am
Without the &, a function just makes a "copy" of a variable's value and uses it for calculations, and typically returns a new value. The & means the function will work with the original variable contents directly, so in that function any changes to a and b would persist after the function is finished.

This post was edited by Mastersam93 on May 8 2015 11:03am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
May 8 2015 11:31am
Quote (Mastersam93 @ May 8 2015 01:02pm)
Without the &, a function just makes a "copy" of a variable's value and uses it for calculations, and typically returns a new value. The & means the function will work with the original variable contents directly, so in that function any changes to a and b would persist after the function is finished.


Good job on not adding anything that has already been said to this topic.
Member
Posts: 1,263
Joined: Feb 11 2008
Gold: 2,650.00
May 8 2015 12:02pm
Quote (AbDuCt @ May 8 2015 12:31pm)
Good job on not adding anything that has already been said to this topic.


That's pretty shallow considering that he expanded answers that were technically correct but incomplete in this context. If the OP doesn't know the difference between passing by value vs passing by reference, do you really think that saying "& gets the memory address of the variable" is going to tell him everything he needs to know? And while the link posted is conceptually almost identical (setting aside possibility of null pointers, etc), it still referred to passing pointers rather than passing arguments by reference, which can be confusing to someone learning this stuff.

If you're still confused, there are a lot of tutorials that will break down the differences. Here's the first that popped up on a google search, which seems to have a number of visual aids if that helps you understand:


This post was edited by postmortemvox on May 8 2015 12:02pm
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
May 8 2015 08:26pm
Quote (postmortemvox @ May 8 2015 01:02pm)
That's pretty shallow considering that he expanded answers that were technically correct but incomplete in this context. If the OP doesn't know the difference between passing by value vs passing by reference, do you really think that saying "& gets the memory address of the variable" is going to tell him everything he needs to know? And while the link posted is conceptually almost identical (setting aside possibility of null pointers, etc), it still referred to passing pointers rather than passing arguments by reference, which can be confusing to someone learning this stuff.

If you're still confused, there are a lot of tutorials that will break down the differences. Here's the first that popped up on a google search, which seems to have a number of visual aids if that helps you understand:
http://www.youtube.com/watch?v=TgsH02sORZ0


Yeah I didn't want to get technical, I just wanted to say what it does, not how it works.

/e And it's worth noting that using & has better performance on classes with multiple member variables, because instead of copying every variable you are just copying one memory address. If you know you aren't going to make any changes to an object's members then you can use & to pass by reference, but it's also good habit to use "const" as well just to make sure you aren't making any changes, like:

int DoSomething(const &object) { ... }

This post was edited by Mastersam93 on May 8 2015 08:28pm
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
May 9 2015 10:07am
Quote (Mastersam93 @ May 8 2015 09:26pm)
int DoSomething(const &object) { ... }


This should actually be:

int DoSomething( const object & x) {...}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll