6 ms·
A meta comment on tutorials to teach pointers: If pointers are so "simple", why are they so confusing?!? Because pointers are one of those concepts that's ea
by jasode 6y ago
A meta comment on tutorials to teach pointers:
If pointers are so "simple", why are they so confusing?!? Because pointers are one of those concepts that's easier to use than to explain.
Some concepts like pointers (and also monads and also English words like "a" and "the") are easier to teach by examples instead of by definitions.
E.g. Consider the English words "a" and "the": a 5-year old can fluidly use those connecting words when talking but a 50-year old adult that's been speaking English for decades will have a tough time defining what the words "a" and "the" _mean_ without giving a confusing and circular explanation. Attempted explanation: the "a" is the indefinite or general signifier while "the" is the definite or specific signifier -- what's the problem?[1]
How did small children learn how "a" and "the" work without being given a formal definition? Because of numerous examples.
By working through enough examples, the lightbulb moment happens ("oh gee, is that all pointers is?!?") and the learner is able to derive his own definition. But the irony is that the learner now has The Curse of Knowledge[2] and he/she now thinks he can write a "better" tutorial to explain pointers but new learners reading that tutorial will still say "I don't understand your explanation at all."
[1] riff on : https://stackoverflow.com/questions/3870088/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem https://stackoverflow.com/questions/3870088/a-monad-is-just-...
[2] https://en.wikipedia.org/wiki/Curse_of_knowledge https://en.wikipedia.org/wiki/Curse_of_knowledge
- melvinroest 6y ago> If pointers are so "simple", why are they so confusing?!? Because pointers are one of those concepts that's easier to use than to explain. I disagree (the rest of your comment I fully agree with, FYI). What I personally found difficult is to understand that: when you dereference an invalid memory address, then the dereferencing operation is working, but the OS/runtime environment won't allow you to do it! Wait, what? Why?! Who cares my value is wonky, why is my program giving an error? Just obtain the wonky value! (is what I thought when I first did it) The reason it's so difficult because at this stage you're still using printf for debugging, while you really should use a debugger, to really understand what's going on under the hood. Preferably, one also learns a modicum of x86 to really see it immediately. The concept of dereferencing the wrong values isn't explained a lot. How to troubleshoot segfaults isn't explained at all in beginner tutorials, not even in my own (I just wrote an explanation for fun as a comment in the same thread [1]). Also FWIW, when I heard the concept of pointers I found them intuitive immediately. I tend to think and reflect a lot, and the explanation of referencing and dereferencing almost sound like some proverbial life wisdom to me. I found their use a lot harder, in part because I found code syntax hard. In another part, because I used to find "doing things" and "building things" (of any kind) an insurmountable task. --- With that said, I do agree with the rest of your explanation. I suppose it also depends on who teaches pointers to you. I got a classroom explanation first, a few examples and then some exercises. Then suddenly, I had to write a 500 line C program. Then all the complexity storms at you like a rabid zombie army. But if you're used to writing small C programs all the time and picked up on pointers that way, but then suddenly have to explain it. Then, I can see how that's hard. [1] https://news.ycombinator.com/item?id=23550040 https://news.ycombinator.com/item?id=23550040
- scoutt 6y agoI think the real difference comes with the knowledge the person is carrying when learning C. For example, people that are familiar with concepts like classes, string objects, arrays, etc. brought from higher level languages are the ones that I see have much more problems when understanding memory management (and thus pointers) in C. For example, for a higher-level developer, a string might mean some kind of object that knows its length, auto-expands, etc. When in C a string is just an array that it will be overflown at the very first chance. Same with other objects. I find that a person that doesn't know much about languages are the ones that better understand the basic concepts of C (like pointers).
- melvinroest 6y ago> For example, for a higher-level developer, a string might mean some kind of object that knows its length, auto-expands, etc. When in C a string is just an array that it will be overflown at the very first chance. Same with other objects. Funny, my programming knowledge in Java helped me because of the whole concept of what a reference is and a literal value. In my first Java programming class, 8 years ago, I learned that you need to have references, otherwise things will take up too much memory. My teacher took a humongous object as an example and asked "what if I need to access this at 10000 different places, but I really only need one physical copy of it?" I was pretty upset that you had to do all of this bookkeeping and accounting. I remember being pretty vocal about that it just didn't make sense to my why one couldn't just copy stuff. I guess we all grow up at some point :) When I saw pointers for the first time (to be fair, years later), I was thinking "ah so a pointer is basically a reference". Other people would say "a pointer is a whole lot more than just a reference, you can do pointer arithmetic with (and so on)". And I was just thinking to myself: alright, it's a reference on steroids, but the groundbreaking essential idea of the pointer is that it refers to something, just like a reference in Java. And then I'd hit a segmentation fault and doubt my own understanding. Learning programming felt emotionally turbulent to say the least, haha.
- patrick5415 6y agoMy experience was nearly the opposite. I tried to teach myself c 15 years ago and found pointers completely bewildering. I didn’t really know how to program any other language then. After a 10 year, non-technical interlude, I went to grad school and spent 5 years writing matlab. In the last year, I decided to learn c again. This time, pointers seemed completely straightforward. The most difficult part for me this round was keeping the syntax straight.
- CivBase 6y ago> pointers are one of those concepts that's easier to use than to explain. My experience is the exact opposite. A pointer is just an address for something. There is a "reference" operator to get a variable's address (pointer) and a "dereference" operator to get the data at a given address. That's it. The hard part for me has always been using them correctly - keeping track of what points to where in my head, especially for void pointers. I haven't done any significant development in C since my early programming years, but that's the part I remember struggling with. They're sort of like GOTO in that they're easy for me to understand, but they can quickly make my code hard to follow.
- nobleach 6y agoBut then you need to explain to a newcomer that you can have `int ptr;` (pointers to pointers)... It's pretty odd for someone coming from say, Python, Java, or JavaScript. (which, many go through years before realizing that pass by reference happens in their language too)
- CivBase 6y agoIs that really any more complicated than arrays of arrays or structs within structs? Pointers to pointers definitely makes things harder to keep track of, but I don't think they're conceptually difficult.
- MaxBarraclough 6y ago> you need to explain to a newcomer that you can have `int ptr;` (pointers to pointers)... I think HN swallowed the asterisks there. I presume you meant: int **ptr; > It's pretty odd for someone coming from say, Python, Java, or JavaScript. (which, many go through years before realizing that pass by reference happens in their language too) That doesn't sound right. Java always passes by value. https://stackoverflow.com/a/40499/ https://stackoverflow.com/a/40499/
- jagged-chisel 6y ago> For objects, the pass by value is the value of the reference to the object. That's exactly what "pass by reference" means. According the SO definition, C is always pass by value: the value of the 'primitive' (int, long, char) or the value of the pointer (an address.) It pretty much applies to any language: ultimately you're passing a value from the caller to the callee. Whether that value is 'raw' (say, an int) or a pointer or reference is all in how you (or your compiler, or your runtime) interpret that value you received.
- attractivechaos 6y ago> pointers are one of those concepts that's easier to use than to explain. Strongly disagree. The rule of pointers is simple: an integer pointing to a memory position. However, using it correctly and effectively is hard. Dealing with pointers is like working with math. The rules are simple but there can be difficult problems coming out of these simple rules.
- jeffrallen 6y ago> easier to use than to explain That is a nice explanation for what I noticed, which was that learning assembly before C meant that pointers were not mysterious to me at all. Just lucky to do it in that order, I guess.