-3

Let we have declaration:

...
int a= 5;
int& b= a;
...

I dont understand, what difference between a and b. I think, that a and b is just adress. But where is this adress in memory? So, let we define a function:

int foo(int& x)
{
    return x;
}
...
foo(a);
foo(b);

What is occuring when foo is calling? I.e. when we'r returning a value we'r going by adress whos in a or b?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • `b` is just another name for `a`. Just think of it as an alias. Anything you do with `b`, you really do with `a`. As for the function, it is `void` so it makes no sense to return anything. – juanchopanza Oct 16 '13 at 13:54
  • 1
    What? Also, you can't return something from a `void` function. – Luchian Grigore Oct 16 '13 at 13:54
  • possible duplicate of [What are the differences between pointer variable and reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c). See also [What value does a reference store?](http://stackoverflow.com/questions/412084/what-value-does-a-reference-variable-in-c-store) – Raymond Chen Oct 16 '13 at 13:58

2 Answers2

4

What happens in your code is that a and b are effectively aliases for the same memory location.

Thus

foo(a);

and

foo(b);

are in effect identical.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

http://www.cplusplus.com/doc/tutorial/pointers/

Read up for more information, things are usually very well detailed in cplusplus.com articles

MangO_O
  • 393
  • 1
  • 3
  • 15
  • 1
    -1: There are no pointers here, and cplusplus.com is a stinking pile. – John Dibling Oct 16 '13 at 14:06
  • @JohnDibling In a sense, there actually are pointers here. First, the OP talks about addresses and second, references are basically nice pointers. Your opinion on `cplusplus.com` is irrelevant here, if you don't specifically target `http://www.cplusplus.com/doc/tutorial/pointers/`. – stefan Oct 16 '13 at 14:15
  • @stefan: The linked page on cplusplus.com says that pointers and arrays are the same thing. That's wrong. I'm sure I could find more errors on that specific page, but I disagree that my observation that cplusplus.com is a poor resource is ever irrelevant. With many errors sprinkled throughout the site, my feeling is it just can't be trusted as a resource at all. Especially by newbies, who simply won't know the difference between good information and bad information. How could they? – John Dibling Oct 16 '13 at 14:20
  • The link actually discusses both pointers and references – mjs Oct 16 '13 at 14:21
  • @JohnDibling Concerning this very bad mistake: It's irrelevant. Neither a pointer nor a plain array should be used, especially not by a beginner. But why cursing ("stinking pile") if you could just _tell them_? Waste of time? Alternatively you could have also just pointed out a better resource: cppreference.com – stefan Oct 16 '13 at 14:30
  • I don't see how its wrong in saying that the variable when you define an array is similar to a pointer. An array will always point to the space in memory where it is stored so it acts as a constant pointer. If you disagree with this could you explain your reasoning? – MangO_O Oct 16 '13 at 21:10