why declare pointer in the first place? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
3 Answers
+ 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
Kinshuk Vasisht
Kinshuk Vasisht - avatar