back

by jeffreyrogers·11y ago·view on hn ↗
When I was learning C I had heard about how terrifying pointers were and so I expected them to be super difficult to learn. Maybe, my experience is atypical, but I found them pretty intuitive.
6 comments
I'm not sure why pointers have such bad rep, they're a pretty simple concept of storing and using memory addresses.

References in dynamically typed languages however often make me nervous, as I'm never entirely sure when different types in different languages get copied or not. If I the function argument p gets modified here, did the original variable in the caller get modified? Do I have to write an elobarate check for its mutability?

I think the difficulty is not so much in pointers themselves but rather the many undefined behaviour landmines that surround them.

Stuff like pointer aliasing, dangling pointers, pointer arithmetics (it's UB to take a pointer after the 1st byte after the end of the object for instance), NULL pointer dereference and things like that. Debugging those kinds of bugs can be super tricky and time consuming.

Not that pointers are the only source of UB in C, but they're probably some of the most easily encountered.

There's also the whole array->pointer decay thing that can be a bit tricky to handle in certain cases (I think the sizeof() a parameter declared as an array is probably a surprise to all C coders when they encounter it for the first time).

I have a similar experience but I have to admit that pointers became reasonably trivial once our teacher told us to use pen and paper (or a digital equivalent) to schematically draw how these pointers work, much like this article does. The difficulty with pointers is, for me, keeping track of them in your head, especially when you work with arrays. Otherwise it's actually pretty straightforward!
> The difficulty with pointers is, for me, keeping track of them in your head, especially when you work with arrays.

I think this is mostly an artifact of C's type system. I wish pointers were taught in a language with similar runtime semantics as C with a more expressive type system. Drawing it out helps a ton, but the way C forces you to embed that structure into code doesn't help.

agree and in the article when he gives advise to his friend, he mentions he has a leg up due to his ASM background. I first learned ASM on a Commodore64 and so I had a very close relationship with the hardware such as indexed addressing and indirect index addressing, etc... By the time I learned C some 10 years later, I already new what authors were talking about on the subject of pointers. Over the years, I wondered how I would have learned this stuff if I didn't have the ASM (assembly language background) and a knack for electronics.

Now a days, things are more complicated with MMUs and virtual memory concepts but I think the examples in the article are a good basis for anyone wanting to know what happens under the hood.

Yep. Once you understand how memory works, it's very straightforward how pointers work (the value of a pointer is actually the address of another "unit" of memory).
True. Never had much problem with pointers in my (limited I admit) C experience.

But I did have a problem with remembering to manage memory clear.