-3

So, I am new to pointers/addresses and I have a generic function that prints the address of a variable passed to it as a parameter. However, in the following snippet why the addresses are different despite they belong to the same variable?

template <typename T>
void printAddressOf(T t)
{
    std::cout << &t << std::endl;
}

int main(int argc, char **argv)
{
    int x = 12;

    printAddressOf(x);
    std::cout << &x;

    return 0;
}

The values that I get as an output are...

0x7ffee2c6f86c
0x7ffee2c6f89c

Can someone explain this behaviour why this happens?

getchar
  • 75
  • 9
  • 8
    It's not the _same variable_, the parameter is a copy, unless you use a reference. – πάντα ῥεῖ Oct 05 '18 at 17:05
  • Change `void printAddressOf(T t)` to `void printAddressOf(const T& t)` and see what happens. Here is the updated program: https://ideone.com/Bgx522 – drescherjm Oct 05 '18 at 17:07
  • @πάνταῥεῖ cool thanks. duh! – getchar Oct 05 '18 at 17:07
  • 1
    you have just discovered what 'pass by value' means. You will understand it much better by discovering it yourself – pm100 Oct 05 '18 at 17:09
  • @pm100 indeed yes. Any directions on how to make such discoveries? Else, will keep on practising and moving forward. – getchar Oct 05 '18 at 17:11
  • @Jash this is the best way to learn. Trial and error and research as to _why_ it didn't work. Don't take what pm100 says to heart. Discussion among other programming peers is a great way to learn and socialize. However _do_ attempt to research why yourself and show that you have made those efforts first. – Dan Oct 05 '18 at 17:13
  • 2
    "Any directions on how to make such discoveries?" - discover from the text of a good book on C++. –  Oct 05 '18 at 17:13
  • 1
    @Maverick 'dont take it to heart' It was meant as encouragement. please do take it to heart – pm100 Oct 05 '18 at 17:15
  • 1
    Here is a list of good C++ books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Eljay Oct 05 '18 at 17:16
  • @Jash write a function that changes the value of x so that main sees the new value. Do it c style and c++ style (pointers and references) – pm100 Oct 05 '18 at 17:16
  • BTW, compilers are allowed to store variables in registers, in which case they have no address. However, taking an address of a variable tells the compiler it can't be stored in a register (depending if the pointer is used or not). – Thomas Matthews Oct 05 '18 at 17:57
  • @Maverick Thank you for your kind words. It really helps. :) – getchar Oct 05 '18 at 21:30
  • @pm100 Lol! True that. I literally need to learn `C++` as for now my life depends on it. – getchar Oct 05 '18 at 21:31
  • @NeilButterworth As @Eljay gave me a whole bunch of books, I am more of looking for one `first` book that I can start with rather than intimidating myself with whole bunch of texts and resources. For sure, I am falling in love with the language and would dive deep but for now I need something that will help me curate some APIs (which aggregate other APIs into a much simpler one) and learn more of generics. Still thanks a ton. :) – getchar Oct 05 '18 at 21:35

1 Answers1

2

Because the variables are not the same, the values are.

In your argument list for function printAddressOf(T t) you are not passing a reference to a variable. Instead you pass a copy. So a new variable T t is created in this scope.

Change the function to

template <typename T>
void printAddressOf(T& t)
{
    std::cout << &t << std::endl;
}

And you will see that the address is the same

printAddressOf(x)             0x7ffee2c6f89c
std::cout << &x << std::endl; 0x7ffee2c6f89c
Dan
  • 1,812
  • 3
  • 13
  • 27