+ 1

why declare pointer in the first place?

we can print address of any variable using-----cout<<&var;...or there is a difference in printing address using pointer and using ampersand(&) only?

3rd Dec 2017, 10:00 AM
Gagan Narang
Gagan Narang - avatar
4 Respostas
+ 16
Nobody uses pointers just to print stuff with cout. Pointers have much more important uses. http://math.hws.edu/eck/cs225/s03/binary_trees/
3rd Dec 2017, 10:12 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
If you just want to print address, then yes it is useless, but if you want for example to have the exact same element stored at different place but those places can be initialized empty, you need pointers
3rd Dec 2017, 10:08 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
No, there is no difference at all. Its just that a pointer is a variable that can store the address so that you may use it later for memory management or other uses. You can change the address of a pointer variable to something else (Some other variables' address), but you cannot do so with the addressof operator (&a), as the value returned is an rvalue.
3rd Dec 2017, 10:08 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar