#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";
}
}
i’m start learning pointer and vector but these topics got me confused sometimes.
user25173387 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0
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.)