3 ms·
I've never understood what's confusing about pointers...
by alexchamberlain 14y ago
I've never understood what's confusing about pointers...
- mkopinsky 14y ago1) Indirection. Pointers require thinking in a few steps. Indirection is hard. It is a vital skill in any kind of programming (or especially debugging), but it does not come naturally to people who've only ever had to deal with the concrete. 2) Early on, you learn that ints and chars and floats and some_structs are fundamentally different data types. Then suddenly you're told that int%s, char%s, float%s, some_struct%s, and even void%s are fundamentally the same. Huh? 3) The fact that C uses % both to create a pointer and to dereference. These are conflicting meanings, and the unrelatedness of those two concepts is not sufficiently explained. EDIT: Silly HN parser. I now replaced asterisks with %s.
- alexchamberlain 14y ago3) That make's sense. For example... int *i; means "When you dereference i, you get an int."
- mkopinsky 14y agoWhen I see int a; I read "create an integer variable called a". int *i; means, "create a variable that when dereferenced, points to an integer". Makes sense once you understand it, but I can definitely understand how a beginner would find it confusing.
- alexchamberlain 14y agoIt may be a problem with the teaching though, rather than the subject matter itself. I understand why it takes a little bit of thinking to get used to, but not why it is fundamentally hard. Now, algorithms can be difficult to understand and many of them use pointers... Do some people confuse the 2?
- mkopinsky 14y agoI don't think the difficulty is fundamental, and I don't think anyone here claimed that it was. But it is initially challenging nonetheless. To tie this back in to the beginning of the thread, being able to understand indirection (such as in pointers and algorithms) is a far more important skill than understanding OOP.
- deleted 14y ago[deleted]
- InclinedPlane 14y agohttp://homepages.tcp.co.uk/~nicholson/alice.html http://homepages.tcp.co.uk/~nicholson/alice.html
- d--b 14y agoIt's the abstraction that's difficult to get in the first place. In Basic, a variable represents at the same time the data and its memory address, the two concepts are not separated. When you introduce pointers, you split that concept in two: address and data, and to complicate you say address is some form of data. In Basic, the variables are the memory. Each variable you manipulate is equivalent to a piece of memory you can store things in or get things out. With pointers, you loose that equivalence: memory becomes invisible through the code. I can have a memory block that exists in my memory that is no longer pointed by any variable. This means that you get a separation between the way memory is and the way the code looks like. What is difficult when learning pointers is to get that extra level of abstraction: you have to look at the code as an access route to the memory, not as the memory itself.