-4

when I looked it up on wiki, it said it was a physical address of a data. but that meaning doesn't make sense in the context the word was used in my textbook:

"Table 6.1 shows the effect of the address-of operator & on the data type of reference."

this doesn't make sense because the expression WITH the & would be the physical address, and hence the reference.

also, they called both valp, &many "references". if they meant the address, only &many would be a reference.

what is the meaning of reference in this situation?

user81055
  • 441
  • 2
  • 5
  • 5
  • 2
    a reference is an implicit address. From the code it looks like a value but it is tied to (an alias for) another variable through the address. In contrast, a pointer is an explicit address. – perreal Jun 22 '13 at 01:53
  • 1
    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) – perreal Jun 22 '13 at 01:54

3 Answers3

2

In a broad sense, there are two main ways to pass values to a subroutine: pass by value and pass by reference. The difference is that in pass by value the subroutine gets a copy of the data so it can mutate it without changing the original while in pass by reference changes to the data in the subroutine are also visible by the caller afterwards.

The reason you are confused is that there are two ways to pass things by reference in C++: pointers and references. Effectively, a C++ reference is just like a pointer except that you don't need to use & and * on it all the time (you also can't do pointer arithmetic on references)

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • References can exist without Call/Pass-by-Reference (which does utilize References in C++, but that's almost secondary). – user2246674 Jun 22 '13 at 01:58
  • 1
    Good explanation though I'd mention one other difference, the fact that a pointer can point to "nowhere" while a reference _must_ have an object behind it. That's related to your "pointer arithmetic" point but probably different enough to warrant its own comment. – paxdiablo Jun 22 '13 at 02:12
1
int a = 1;
int &b = a;//b is an alias for a.
a = 2;// b will also be 2

It's a C++ feature (because I saw you first tag the question with c), in the simple example above, b is the reference of a, so its value will be modified accordingly.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

Your question entangles more than one misconception; so, to start you in the right direction:

The & operator in C++ means reference to in some contexts and address of in others. These two meanings are only vaguely related, and it seems that your question conflates the two. You will probably have to do several more hours of reading to unconflate the two meanings in your mind before you can ask a meaningful question in the matter here.

Good luck.

thb
  • 13,796
  • 3
  • 40
  • 68