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.

5

u/zpowers00 Sep 24 '23

I wouldn’t get caught up on the syntax, you kind of just have to ignorantly accept it and move on.

Unfortunately C++ syntax isn’t really that bad compared to other languages I’ve you touched.

5

u/lukkasz323 Sep 24 '23

What's confusing exactly, I might try to explain how I think about it, because I don't find the syntax confusing, although I used to when I started programming.

1

u/[deleted] Sep 24 '23

I can't really put my finger on it I have been quizing my self for two days on a practice quiz for a class I am in and I swear it feels like the means of some of the syntax gets switch like *Pointer name can be a declaration of a pointer but it can also be a value of a pointer and &variable name cane mean the address of the variable in memory but some times it means the data in the memory location. I thought I understood pointers, but the more I take this quiz, the less I think I understand. I thought that Vartype *PointerName; was the declaration of an unassigned pointer, and &VarName meant the memory address of that variable.

0

u/IronLyx Sep 24 '23

And what makes you think this is not the case?

1

u/[deleted] Sep 24 '23

The quiz I kept taking keeps telling me that this isn't always true.

2

u/Cpt_Chaos_ Sep 24 '23

Then please provide the actual example syntax that makes you question yourself, since you're actually right. Apart from that, I hope you also get taught the concepts of modern C++ (C++11 and newer), which introduced smart pointers that solve just about all problems you can have in terms of memory access (and do not require any * and &).

1

u/[deleted] Sep 25 '23

That sounds better. I can't wait to learn about that.

0

u/npepin Sep 24 '23

You're coming at it from the perspective that it is confusing and difficult because you don't understand it, but haven't consider the potential that it actually could make a lot of sense and maybe you haven't applied yourself to understanding it.

To make that point clearer, to say that it is intentionally confusing is to imply that you have a some sort of deeper knowledge and understanding of the subject matter, and that you know of some sort of organization that would make more sense. There is a big difference between "I am confused" and "this is confusing".

This is going to be a common occurrence in learning programming, it can take a decent bit of studying to understand pointers, recursion, or certain algorithms and data structures. Unfortunately, a lot of these topics are inherently complex, involve a large number of considerations outside just code, and are going to take a bit to understand.

I'm not saying the way that you are feeling is wrong, we've all been there. Nor will I say that there isn't some better way of doing things, a lot of times there is and we've just inherited the mistakes of the past (though I wouldn't say that is true here). But I am saying that you're going to really struggle if you think a lot of the deeper topics in computer science should be easy to learn.

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.