+ 3
When do we use pointers in c++ ?
10 Answers
+ 9
-to allocate new objects on the heap
-to pass functions to other functions.
-to iterate over elements in arrays or other data structures.
-to pass array as function argument or return an array
+ 8
JaScript
beg to disagree.
c++ have lots of other options. Why use c++ if you are going to write C code?
I would say, use pointers only if you absolutely need them.
Here is a Stackoverflow topic:
https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself
+ 4
I've been learning programming myself for a relatively short time and I think that there are people here in the community who studied and closed it at university and have the most knowledge about it. Hopefully they will contribute something soon here.
The question Bob_Li quotes was asked and answered a good 8 years ago and I would only consider it as an opinion.
There are many situations, where you need acceess / change data memory contents such as arrays, customised data structures etc. From other learning sources and my small experience, it is seen as an advantage to build the algorithms this way using pointers. The best to understood why that is better can be showing with pictures, not with words.
By the way, if you want to examine your solution paths, you can simply measure the execution time of your algorithm and decide what you take.
+ 2
As often as possible, because it is faster then most other solutions.
+ 2
JaScript
True, you can optimize your code if you dive deep into pointers, but that depends on your use case.
Ask yourself what your code is going to be used for? Is the gain you get for optimization worth the effort?
And cryptic codes are things no one wants to touch.
Ever heard of the quote "Premature optimization is the Root of all Evil"?
I would think writing everything in pointers falls in that category.
But if you really must, then go with pointers, sure.
https://stackify.com/premature-optimization-evil/
+ 2
JaScript
While studying, pointers had 80% of my classmates drop out or fail C++ entirely.
It's an awesome feature which with any power comes great responsibility, the good and the bad.
You could declare a pointer variable to hold the address in memory for a huge 1 GB array. Passing the pointers to and from classes or functions carries less weight than the actual value, increased speed and efficiency.
Hopefully cleaning up after the use of such variable is complete, as leaking memory is not recommended.
+ 1
Bob_Li thanks <3
+ 1
JaScript well ... Is there's any particular ideas where we use them ? Or it's just about how we solve the problem ?
+ 1
Zakaria Elalaoui
-to use C instead of wasting time to learn C++đ
I mean, why bother with C++ at all. If you want total control, C is sufficient.
+ 1
pointer refer to a variable which hold the address of another variable Like regular variables, pointers have a data type.
It also point to function or array
Example:: int*p =&a;