0
why use pointers
3 Respuestas
+ 17
You can use it to DIRECTLY edit the value of an original variable passed by reference to a function without returning an edited copy or any other workarounds (...Regardless the scope...) ....
+ 1
basically pointers point your variable, and contain variable address.
int a=5, p
*p=&a;
cout<<p;
cout<<&a;
cout<<a;
+ 1
Because by default, if you call a function with an argument from another function, that argument WILL GET copied rather than passing a reference. Needless ro say, if you work with huge classes and structs (or arrays with 1000+ elements) the copying could take more time than the function it self.
Not to mention that everything you do to that passed parameter will NOT be saved to the original (hence the copy).
On the other hand if you pass it with a pointer you are effectively working with the original array/class.