57 ms·
Understanding this little subtlety of the pointer declaration syntax was the epiphany that led me to feel like I actually, truly understood pointers a couple ye
by FaceKicker 15y ago
Understanding this little subtlety of the pointer declaration syntax was the epiphany that led me to feel like I actually, truly understood pointers a couple years ago (never been much of a C programmer). This syntax always confused me - I couldn't figure out why it wouldn't be something like "int& p" to declare an int pointer, but rather includes the dereferencing symbol, which made it seem anti-pointy to me at the time. Pretty much the instant I figured out why my thinking was wrong - that "int * p" should be read as "(* p) is an int" rather than "p is an (int *)" (as this post explains nicely) - pointers just completely "clicked" for me.
- eru 15y agoIf you had started with something more sane, like Forth or I guess even flawed Pascal, you would have understood pointers much earlier. C is such a horrible language to learn first, second or even third.
- dbattaglia 15y agoWhat about learning C first compared to languages that don't have the concept of real pointers (like, say, Java)? I started out with C++ (and still write C/C++ from time to time), and I feel like it's given me a better understanding of memory management and reference types that a lot of programmers I meet and work with don't seem to really grasp. In C# code I constantly see the "anti-pattern" of new-ing up a variable at declaration, only to immediately overwrite it on the next line with the results of some function. I can't help but think that the person writing it has no concept of what "new" is actually doing (with regards to the heap and the GC, for example).
- eru 15y ago> What about learning C first compared to languages that don't have the concept of real pointers (like, say, Java)? If you want to learn pointers, you have to learn a language that provides them. So C, or like I said, Forth or Pascal. C is fine as a first language, if you don't mind the pain. C++ is a horrible language, no matter when you learn it. Never cripple the minds of beginners with it. About your C# anecdote, does the language allow something like the following?: type variables = someFunction (); Nowadays I usually program in Haskell for my day job. There lots and lots of nice concepts to be understood there. You could even use pointers, if you are desperate enough.
- dbattaglia 15y agodoes the language allow something like the following?: yes, exactly. The problem is you see stuff like this: var obj = new SomeType(); obj = GetSomeType(); Basically creating 2 objects, the first of which is immediately garbage since it no longer has any roots. Anyone who understands pointers/references and memory allocation should understand that "new" is actually creating a new live object on the heap, rather than just initializing a pointer to null (or a stack-based value type to zero). These days C# is actually a decent language to code in, it has a nice functional aspect to it with linq/lambdas, and you can still go down to C style pointers/"unsafe" code if necessary (with lots of restrictions of course). It's really the Microsoft dependency that kills it more than anything else.
- eru 15y agovar obj = new SomeType(); obj = GetSomeType(); You could almost be tempted to write an automatic converter to var obj = GetSomeType(); but you can't in general, because you don't know whether `new SomeType()' doesn't have side-effects. But if it has some that are relied on, that's probably a bug. I know that there's a free .net runtime called Mono. What about the compiler side of free C#?
- badboy 15y agoI started with C as my first language and even though pointers made some code really hard to read and sometimes my own experiments with pointers crashed horribly, it helped me understanding programming in a whole, how the memory is used and changed, ... I no longer write anything real in C now, but I feel quite confident when reading other's people C code.
- eru 15y agoI learned C pretty early on. But I wouldn't want to inflict that on other people.
- kstenerud 15y agoMy first language (after BASIC) was assembler, after which I moved to C, so pointers in C weren't a problem. The re-use of symbols and keywords for different things was a pain, however, and got even worse in C++.