r/learnprogramming • • Sep 24 '23

Pointers.....why

I am learning C++. Honest question why are pointers a thing and why did they make the syntax surrounding it so confusing. Was it made to be intentionally confusing? Was it made to make this more difficult.

0 Upvotes

49 comments sorted by

View all comments

1

u/zpowers00 Sep 24 '23

A pointer is like a drawer to a desk. To put stuff in the drawer you’ll want to use the pointer to get to the drawer. If you didn’t use a pointer, then you’d have to get a new drawer on the other side of the building and then put stuff in it and then drill it into your new desk which is a lot of work. Just use the same drawer to put your stuff into.

0

u/[deleted] Sep 24 '23

But why is the syntaxes around it so confusing why use the same symbols in different spots to mean different things instead of just using different symbols? Why make it intentionally confusing.

1

u/DavidJCobb Sep 24 '23 edited Sep 24 '23

why use the same symbols in different spots to mean different things instead of just using different symbols?

The only symbols they reuse for pointers are & and *.

As for why? There are only so many symbols available on a typical American keyboard. Off the top of my head, the only symbols from that keyboard layout that C and C++ don't use are @ and $. C was developed in tandem with Unix, and in old Unix text editors from way back in the day, @ and # were shortcut keys for deleting text. This made @ dangerous to use at all, and # dangerous to use anywhere but the start of a line.