As someone new to programing (C++) , I came across pointer which gave me a headache doing it as I never seen the point of using it. I tried to look at other answer on stack overflow but it was a bit confusing.It would be great if someone could explain why we use pointers and in a way to make it easier to understand.
-
Why do we use objects in java? – Guillaume Racicot Sep 12 '18 at 03:28
-
A pointer tells you where something is, like an address or a phone number and many other things that you encounter every day (and probably find very useful). You already know much more about them than you think, you just haven't thought of things as "pointers" before. – molbdnilo Sep 12 '18 at 08:45
2 Answers
One reason is so that we can directly manipulate the content the pointer address is holding.

- 221
- 3
- 9
There are a number of uses for pointers. They represent a way for code to manipulate memory directly primarily, but can also be used for things like arrays. In modern C and C++, you would usually use pointers for accessing hardware to control it through custom struct
s, for example.
However, for some other complex and for recursive types, pointers are still the easiest and only way to model them. A linked list, for example, could have any number of items, but there's no really good way to refer to the next (or previous) item in C or C++ without using a pointer to it.
Allocating larger data structures will also usually require a pointer to refer to it as well, since these large allocations won't fit on the stack.