-2

consider the example

int value(int &a)
{
    int z;

    z=a;

    return z;
}

My Question is why it return the value of a not the address since &a refers to its address not the value.it should assign the address of parameter to z and returns the same.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • It returns a value because that is what the `int` return type means. And `&a` is not an address. – juanchopanza Nov 22 '14 at 08:44
  • Reference to a variable likes new name for variable. It contains value of variable not address. – Ali Mofrad Nov 22 '14 at 08:47
  • @AliMofrad I'd rather say a reference does contain the address, but evaluates to the value under that address. – Kos Nov 22 '14 at 08:51
  • Take a look at this answer: [pointer vs ref](http://stackoverflow.com/questions/334856/are-there-benefits-of-passing-by-pointer-over-passing-by-reference-in-c?rq=1). Sometimes it's better to examine this kind of thing when discussing class types rather than simple types like int, which I feel confuse the issue due to its size and triviality in the context of performance. – mungflesh Nov 22 '14 at 09:00
  • Sorry ... missed the edit time window above ... @Ghulam - try your example again with a struct or class type and see what issues you encounter. They might help clarify a few things. – mungflesh Nov 22 '14 at 09:07
  • @mungflesh That really would make no difference. OP's issues are more basic (not understanding the C++ syntax for reference types and the semantics of return by value.) – juanchopanza Nov 22 '14 at 09:35
  • @juanchopanza - speaking from experience, years ago, the eureka moment when I knew I understood references hit me from seeing their worth when passing large structs or class as a const reference. I can see the OPs issues are pretty fundamental, I was just suggesting a different approach in case it helped them in the same way it helped me. And your suggestion to help is ... ? – mungflesh Nov 22 '14 at 10:00
  • @mungflesh The semantics are the same, regardless of the size of the type. In my opinion, the easiest way to understand references is to see them as an alias for an object (which is what they are.) – juanchopanza Nov 22 '14 at 10:03

2 Answers2

0
int value(int &a)

This is syntax for accepting argument as reference i.e an alias for the parameter passed to this function rather than an address for that variable.

If you want to accept address of the passed argument then you should use:-

int value(int *a)  << This would store the address of passed argument.
ravi
  • 10,994
  • 1
  • 18
  • 36
  • Your last statement could be confusing for someone who has difficulties with the concepts anyway: you don't get the address of the passed argument, but the passed argument has to *be* an address to something. If you want the address of the passed argument, you have to accept it by ref and then take the address within the function. – Oguk Nov 22 '14 at 10:13
0

The & symbol has multiple meanings in C++. In this context (declaration) & does not means address of it means reference to. It acts as an alias of the passed in variable such that changing the alias changes the variable itself.

This is completely different (though in some ways similar) to declaring a pointer to a variable that holds its address which is taken using the address of operator (also &).

So it is all about the context or how the & is used:

declaration:

int& i = n; // i is an int reference to n (& means reference)

operator:

int* i = &n; // i is an int pointer to n (& means address of)
Galik
  • 47,303
  • 4
  • 80
  • 117