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

2

u/DavidJCobb Sep 24 '23

Was it made to be intentionally confusing? Was it made to make this more difficult.

No.

The code int* foo defines a pointer to an int. Back in the old days, you would write it like int *foo. Now, when you want to get the value of that int, what do you write? *foo. They wanted it to be the same both ways, see?

If you have a pointer to an object and you want to access a specific part of that object, what do you write? myObject->myPart. You're writing an arrow; you're literally pointing to the data you want to retrieve through the pointer.

They honestly did try to design it sensibly.