0

Pointer lesson is slightly hard .Help me

Where the pointer is used? What is the point application(usage)?

15th Feb 2018, 10:00 AM
ali Hosseini
ali Hosseini - avatar
2 Antworten
+ 4
You know how arrays are indexed with numbers? int[3] arr = {14, 18, 30}; int i = 1; arr[i]; // 18 You could now carry around just the index variable i and you would know to plug it into arr the get the result when you need it. Now imagine all the gigabytes of memory on your computer as a single huge array. A pointer `int *p` is now like the index variable i, and doing `*p` is looking up the value in memory like we did above with `arr[i]`. As alex said, we use pointers for big objects, and also objects that are supposed to be used for a long time.
15th Feb 2018, 11:20 AM
Schindlabua
Schindlabua - avatar
+ 4
Basically: Use value if you can and pointers only if you must. You could say that you only use pointers if the value/object is too big to pass it to a function or method, since it creates a copy of itself in the function/method. Without going much into detail: it wastes resources and can eventually crash your program. This problem doesn't really apply to basic example codes, but just imagine an object with tons of information stored in it. When you get more into classes you can ask again for more details. But for now this should be enough I guess.
15th Feb 2018, 10:47 AM
Alex
Alex - avatar