3 ms·
Pointers are by far the most insidious thing about C. The problem is that nobody who groks pointers can understand why they had trouble understanding them in th
by andrewla 4y ago
Pointers are by far the most insidious thing about C. The problem is that nobody who groks pointers can understand why they had trouble understanding them in the first place.
Once you understand, it seems so obvious that you cannot imagine not understanding what a pointer is, but at the beginning, trying to figure out why the compiler won't let you assign a pointer to an array, like `char str[256]; str = "asdf"`, is maddening.
One thing I think would benefit many is if we considered "arrays" in C to be an advanced topic, and focused on pointers only; treating "malloc" as a magical function until the understanding of pointers and indexing is so firmly internalized that you can just add on arrays to that knowledge. Learning arrays first and pointers second is going to break your brain because they share so much syntax, but arrays are fundamentally a very limited type.
- Koshkin 4y agoWhat is so difficult about the concept of a memory address? Is it the C syntax? Asking because I personally have never struggled with this.
- andrewla 4y agoThat's the problem! I can't tell you what is difficult because it seems so incredibly obvious to me now. When I was ~12, I had a lot of trouble with it, and the only thing I remember from those times is various attempts to figure out why the compiler wouldn't let me assign a string value to an array. What the hell is an "lvalue", Mr. Compiler? Now I look at the assignment command above and I recoil in horror, but for some reason at the time it seemed very confusing to me, especially since `char *str; str = "abcd";` works so well. The different between the two (as far as intention goes) is vast in retrospect, but for some reason I had trouble with it at the time.
- rando14775 4y agoThe pointer/array confusion in C makes this way harder to understand than it has to be. The other thing is the syntax, which is too clever and too hard to parse in your head for complex expressions. Both of these things also tend to not be explained very well to beginners, probably partly due to the fact that explaining it in detail is complex and would perhaps go over the beginner's head. It's also stupid, so you'd probably have to explain how it turned out to be this complex.
- DougBTX 4y ago> Is it the C syntax? Pretty sure that this is a big factor, I’m not aware of any recent languages that put type information before and after the variable name. Nowadays there’s always a clear distinction between the variable name and the type annotation.
- chasil 4y agoWhen I've had to explain it, I describe memory as a street with house numbers (which are memory addresses). A house can store either people, or another house number (for some other address). If you use a person as a house number, it will inflict grievous harm upon that person. If you use a house number as a person, it will blow up some random houses. Very little in the language stops you from doing this, so you have to be careful not to confuse them. Then I describe what an MMU does with a TLB, at which point the eyes glaze over.
- patrick451 4y agoFrom my memory, the syntax of pointers really tripped me up. E.g., the difference between * and & in declaration vs dereferencing. I think this is especially confusing for beginners when you add array declarations to the mix.
- abareplace 4y agoAgreed. Complex declarations (e.g., array of pointers to functions) are non-intuitive: http://www.ericgiguere.com/articles/reading-c-declarations.html http://www.ericgiguere.com/articles/reading-c-declarations.h... https://cdecl.org/ https://cdecl.org/ I learned this from the book Expert C Programming by Peter van der Linden.
- teo_zero 4y agoI don't remember C using & in declarations. Is it a recent addition?
- int_19h 4y agoFor this example: char str[256]; str = "asdf" both str and "asdf" are not pointer-type expressions; they're both arrays (which is exposed by sizeof). The reason why this doesn't work is because C refuses to treat arrays as first-class value types - which is not an obvious thing to do regardless of how well you understand pointers or not. Other languages with arrays and pointers generally haven't made this mistake.