4 ms·
When I was first learning C, pointers scared the crap out of me, and it took me longer to understand them than I am comfortable admitting to. Then I got pointe
by krylon 12y ago
When I was first learning C, pointers scared the crap out of me, and it took me longer to understand them than I am comfortable admitting to.
Then I got pointers and this big "NOW I get it"-moment and felt really clever.
And then came the moment I had a problem with pointers and arrays being interchangeable most of the time and became very confused about when they were not equivalent.
People who like C (which I do, too) will often point out C's perceived simplicity when compared with, say, C++, but C definitely does have its murky corners, and the need for backward compatibility with existing code will most likely prevent them from getting cleaned up.
- lfowles 12y agoMuch of my initial confusion with C style pointers was using & to show pass-by-reference and to get the address of a variable. It still doesn't make sense, but at least I've got it deeply ingrained now :\
- pikachu_is_cool 12y agoC doesn't have pass by reference.
- krylon 12y agoNo, it does not. That is why pointers are so important in C. But to a newbie, the difference between a local variable and a pointer to it can be rather confusing, and the fact that with arrays this difference becomes rather blurry unless one has a good mental model of what the compiler turns your code into, makes it even more confusing. Given that some data structure such as linked lists are highly inconvenient or even impossible to implement without dynamic memory management, one is pretty much bound to writing code that is either primitive (not always a bad thing) or horrible to look at (and maintain) until one understands pointers.
- ArkyBeagle 12y agoIt has "pass a pointer by value", which works out conceptually to the same thing, only with slightly different syntax.
- pikachu_is_cool 12y agoYeah I was talking about this: > was using & to show pass-by-reference
- Buge 12y agoC++ hates creating new words so they try to reuse old stuff as much as possible with new meanings. There are 3 meanings of static, 2 meanings of auto, 2 meanings of delete, 2 meanings of default.
- eliben 12y agoThis is not limited to C++. All widely-used, "old" programming languages shun away from adding keywords, because it can break previously working code. Python is the same way too.
- userbinator 12y agoI'd say that in the case of pass-by-reference, the symbol they reused makes sense: references are basically syntactic sugar for a limited form of "automatically-dereferencing pointers", meaning that creating a reference involves taking an address, and so & would be the logical choice. Personally I don't think it's so useful of a feature; it saves typing a few asterisks but obscures the fact that you're actually modifying some other variable.
- davvid 12y agoPersonally I don't think it's so useful of a feature; it saves typing a few asterisks but obscures the fact that you're actually modifying some other variable There are many real-world use cases where references are exactly the right feature, and the const-ness of that reference should make the intentions clear. Performance-critical code is one place where not having the ability to pass large objects around by reference would be a performance killer. References allow us to avoid making unnecessary copies. Yes, you can accomplish the same thing with a pointer, but raw pointers are much less common in modern C++. It's not just syntactic sugar; a reference conveys information. When writing a function you can eschew null pointer checks when using references, whereas a function taking a (raw) pointer should handle the null pointer case since technically you can call foo(nullptr) and it'll compile. With a reference bar must exist for the foo(bar) expression to compile when calling void foo(const Bar& bar), so it's safer, simpler code.
- Buge 12y ago
- ArkyBeagle 12y agoI think the easiest compare-contrast is with Pascal, and 'C' just shows the implementation details of pass-by-reference a bit more than Pascal.
- JoeAltmaier 12y agoI'm the same way! Should have used '@' to create a pointer, and '*' to deref one. They got it all twisted up in 'C'.
- Peaker 12y agoC++ inherited this particular complexity from C, so it naturally falls out of a complexity comparison.
- analog31 12y agoGranted it was a long time ago, but what helped me to understand pointers was learning a simple computer architecture. I happened upon a book on the Z80, that I recall was published by Howard Sams, but might have been sold at Radio Shack.
- IgorPartola 12y agoI never had trouble with this because from an early age I developed a mental model of how memory worked: numbered bytes stored in a long sequence. What can you actually store like this? Well, bytes obviously, and numbers or characters represented by sequences of bytes. But you can also store addresses to locations in this long list of bytes. And you can store an address of an address. I mean, how else could it work, I asked myself. Now a pointer is a location in memory that stores an address to something. What is an array then? It is a pointer to a beginning of a typed sequence (typed as in you tell the compiler how to treat each subsequence but that is fluid because of casting). So a[1] and *(a + 1) are the same thing. C is simple in that it always deals with sequences of bytes, nothing more.
- krylon 12y agoThere is a quote (I forget who it is attributed to), that "Unix is simple, it just takes a genius to understand its simplicity" or something like that. If you come from a bash/perl background to C, suddenly being so close to the bare metal can be overwhelming. Most languages go out of their way to hide how the CPU and memory actually interact. C (and C++ to the degree that it is C's offspring) is the only language that makes it this explicit. And even so, many C tutorials and textbooks will try to distract readers from this, for various reasons. I've met a few other programmers that also found pointers confusing and intimidating at first, and they, just like me, had this moment when we finally got it, and suddenly it was so simple, and we wondered how we could ever have been so stupid not to understand this. It's a mild version of the enlightenment Lisp hackers must experience when they finally get it. ;-)