0

Why we need pointer??? Why we need to know the address through pointer???

3rd Mar 2019, 6:00 AM
Mohammad Saidul Arefin Taibin
Mohammad Saidul Arefin Taibin - avatar
2 Respostas
+ 1
When you pass an argument to a function, a copy is made of the value. That can take time and memory. If you use a pointer it's like saying to the function: Don't copy it, just look where it's already written in memory. A bit like when you ask an old question in q&a and mods tell you to use the search function. 😏
3rd Mar 2019, 8:24 AM
HonFu
HonFu - avatar
0
Basically pointer gives you access to something called heap, so that is some extra storage for you. Also as HonFu says you can pass parameters to the function by actually not making a copy of the value. You pass the address of the value so that when the value gets modified in the function body, the value outside the function is effected as well. Take C++ vector as an example. You can almost completely forget about array and use vector (even name the vector arr) in your code, as it allows dynamic size. Vector is made possible thanks to pointers as well.
3rd Mar 2019, 8:54 AM
Ah Zong
Ah Zong - avatar