#include <iostream>
#include <vector>
int main ()
{
std::vector<int> num {1, 2, 3};
for (const int &i : num)
{
std::cout << i <<"\n";
std::cout << &i <<"\n";
}
}
The variable i
is a reference, not an object. This is a bit of a special thing in C++. References are not objects, and so there is no such thing as "the address of i
" as you're asking. The value of expression i
, in which you name the reference, is the object which is bound to the reference. Therefore, &i
is the address of the vector element. (You should see the address values increasing arithmetically, since the vector stores elements contiguously.)
If you wanted to see the address of a local variable, you could change i
to be an object variable, like this:
for (int i : num)
{
std::cout << i << "\n";
std::cout << &i << "\n";
}