Is there any way to find the address of a reference?
Makingit more specific: The address of the variable itself and not the address of the variable it is initialized with
Is there any way to find the address of a reference?
Makingit more specific: The address of the variable itself and not the address of the variable it is initialized with
Just use the '&' operator. e.g :
int x = 3;
int &y = x;
cout<<&y<<endl;
This will return the address of x since y is nothing more than the address of x.
Not reliably, as references don't have to have a unique location in addressable memory.
References don't have their own addresses, although references may be implemented as pointers, there is no need or guarantee of this.
Parashift says it best:
Unlike a pointer, once a reference is bound to an object, it can not be "reseated" to another object. The reference itself isn't an object (it has no identity; taking the address of a reference gives you the address of the referent; remember: the reference is its referent).
Please also see my answer here for a comprehensive list of how references differ from pointers.
The reference is its referent
No.
As Bjarne Stroustrup says in TC++PL, a reference can be thought of as just another name for an existing entity (object or function). While this is not always the most precise description of the underlying low-level mechanism that implements references, it is a very good description of the concept the references are intended to implement at the language level. Not surprisingly, the language provides no means to obtain the address of reference itself.
At language level reference is not guaranteed to occupy a place in storage, and therefore in general case it has no address.
The ISO standard says it best:
There shall be no references to references, no arrays of references, and no pointers to references.
I don't like the logic a lot of people are using here, that you can't do it because the reference isn't "guaranteed to be just a pointer somewhere anyway." Just as int x
may be only a processor register with no address, but magically becomes a memory location when & x
is used, it still may be possible for the compiler to allow what you want.
In the past, many compilers did allow exactly what you're asking for, eg int x, y, &r = x; &r = &y;
. I just checked and GCC will compile it, but with a strongly worded warning, and the resulting program is broken.
Not by itself. If you want its "address", shove it in a struct or class. Even then that isn't necessarily guaranteed to get you within the vicinity of what you probably want to do which is using a pointer. If you want proof, the sizeof of a reference is equal to the referent type. Try it with char & and see.
NO. There is no way to get the address of a reference.
That is because a reference is not an object, it is an alias (this means it is another name for an object).
int x = 5;
int& y = x;
std::cout << &x << " : " << &y << "\n";
This will print out the same address.
This is because 'y' is just another name (an alias) for the object 'x'.