3 ms·
I 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
by IgorPartola 12y ago
I 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. ;-)