+ 1
When do we use pointer? I don't get when to use it.
6 Respostas
+ 1
I've used it when I want to change a global variable from inside a function, but want the variable that I'm changing to be an input for the function. If you just type the variable name into the input, then it'll take the value of the variable, change it, then discard it at the end of the function. If you use a pointer as the input, it will take the variable and change its value. I actually just used this to make make my program about 50 lines shorter. Very useful.
+ 1
When you want multiple returns, for example. Or when you simply want to change the value of a variable. If you send a pointer to a variable, you can change it inside a function, and this change will be made to the original variable. If you send a variable, C++ compiler will simply make a copy of your variable. Original won't change.
If you work with a large array, for example, making a copy of that array will take lots of memory. If you send a pointer, you won't need as much memory. But you risk damaging (making permanent changes to) the original array...
0
When we need to use address of the memory location we use pointers
0
it makes calculations faster ,using d address of d value
0
on dynamic programming, like arrays you must have knowledge of pointer.
cause on dynamic arrays using function in c++, parameter function of arrays use pointer.
0
Aside from referencing and deferencing, one of the most essential uses of pointers are for dynamic memory allocation. Dynamic memory allocation can only be done using pointers. And this dynamic memory allocation has many applications including, but not limited to, game development and artificial intelligence.