Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"&" meaning after variable type [duplicate]

Tags:

c++

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
What's the meaning of * and & when applied to variable names?

Trying to understand meaning of "&" in this situation

void af(int& g)
{
    g++;
    cout<<g;
}

If you call this function and pass variable name - it will act the same like normal void(int g). I know, when you write &g that means you are passing address of variable g. But what does it means in this sample?

like image 321
vico Avatar asked Jul 22 '12 21:07

vico


People also ask

What is quotation mark used for?

The primary function of quotation marks is to set off and represent exact language (either spoken or written) that has come from somebody else. The quotation mark is also used to designate speech acts in fiction and sometimes poetry.

What does 2 with quotation marks mean?

Double quotes are used to mark speech, for titles of short works like TV shows and articles, as scare quotes to indicate irony or an author's disagreement with a premise. In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech.

What is the 1 quotation mark called?

Single quotation marks are also known as 'quote marks', 'quotes', 'speech marks' or 'inverted commas'. Use them to: show direct speech and the quoted work of other writers. enclose the title of certain works.


2 Answers

It means you're passing the variable by reference.

In fact, in a declaration of a type, it means reference, just like:

int x = 42;
int& y = x;

declares a reference to x, called y.

like image 118
Luchian Grigore Avatar answered Oct 20 '22 10:10

Luchian Grigore


The & means that the function accepts the address (or reference) to a variable, instead of the value of the variable.

For example, note the difference between this:

void af(int& g)
{
    g++;
    cout<<g;
}

int main()
{
    int g = 123;
    cout << g;
    af(g);
    cout << g;
    return 0;
}

And this (without the &):

void af(int g)
{
    g++;
    cout<<g;
}

int main()
{
    int g = 123;
    cout << g;
    af(g);
    cout << g;
    return 0;
}
like image 23
cegfault Avatar answered Oct 20 '22 08:10

cegfault