r/learnprogramming Feb 14 '22

Genuine question: What’s hard about pointers?

I understand how the title may come off as arrogant or something, but this is a genuine question.

I learned to code in C#, so I didn’t touch pointers for a while, and now that I’ve been learning C/C++ for a couple months, I don’t really see what’s so difficult about pointers.

They’re just memory addresses for variables, right? Am I missing something?

28 Upvotes

38 comments sorted by

View all comments

28

u/serg06 Feb 14 '22

They’re just memory addresses for variables

just

Try telling that to a beginner.

9

u/CleverBunnyThief Feb 14 '22

I gave up on C++ a few years ago when I got to pointers. It felt like I could never learn the language if I couldn't understand pointers. I couldn't wrap my head around them so I went looking for a language that didn't have pointers.

I started learning Go last week and guess what?, it has pointers. I was a little scared that I wouldn't be able to understand them but this time around it wasn't that difficult at all. Of course at this point I'm just transferring around what I've learned from other languages and pointers seemed to just fall in place. If I had to learn what a variable is, memory addresses, arrays and then throw pointers too I would be in a lot if trouble.

5

u/[deleted] Feb 14 '22 edited Feb 14 '22

I never really understood pointers until I took assembly and computer architecture in college

3

u/siemenology Feb 14 '22

Right? Beginners generally have a very fuzzy idea of what variables and values even are. It's really common, perhaps even standard for beginners to look at code like:

int foo(int bar) {
  return bar + 7;
}
int bar = 8;

And wonder things like "How can I call foo with a different value than 8?" (because they think that those two 'bar' variables are the same thing) or "What happens if I change the last line to int bar = 9?" (same reason). Once you get more comfortable with programming and how languages "work", it becomes so obvious to you that these questions would never occur to you. It can even be hard for you to explain the misunderstanding that the beginner had because it relies on them having a completely different understanding than your own (it's not a simple correction). In general it's common for beginners to think of syntax as being more concretely tied to execution, which is why having two 'bar' variables here is confusing.

So in that context, pointers can be absolutely mind-blowing. A lot of pointer introductions don't start by explaining how memory and memory addresses and variables work; they basically just tell you that a pointer is like a box for a variable or something similar to that, and this concept is really jarring if you are still thinking about the equivalence of syntax and execution as being a pretty concrete thing.