4 ms·
I agree. I really can't understand what is complicated about them. I find the same with programmers who find the DAG in git difficult to grasp. It's just linkin
by globular-toast 6y ago
I agree. I really can't understand what is complicated about them. I find the same with programmers who find the DAG in git difficult to grasp. It's just linking stuff together. It's how the the web works. It's how linked lists work. What's difficult about it.
I think exposure to assembly demystifies all of this stuff about C. Most of C is just making stuff really convenient that you would do anyway in assembly. A pointer is just a memory address, but in C it also has a type. The only thing the type is for is so the compiler knows how many bytes to add/subtract when you increment/decrement the pointer. That's it. In assembly you'd just have to remember how many bytes to increment/decrement yourself.
Reading Knuth also helps with this. It's one of the reasons he uses a synthetic assembly language rather than a high-level language. There are no pointers. There is just memory, and you need some way to find stuff in memory, otherwise it is just noise.
- dnautics 6y agoIMO I don't think the problem in C is pointers, it's that pointers are arrays, in the sense that they are syntactic sugar. This is incredibly confusing, and probably part of the reason why most modern systems languages wisely make arrays more e.g. {pointer, length}, sometimes introduce slices, where length is runtime, and make you do some syntactic work to pull out the pointer from the array.
- kevin_thibedeau 6y agoPointers aren't arrays because sizeof works differently and pointer arithmetic on a pointer-to-array doesn't work "right". The more confusing part is the variety of ways pointers get used: allocated memory, opaque object handles, extra return values, modifiable input values, array indexing, function pointers, etc.
- coliveira 6y agoNo, pointers are not arrays. Pointers can point to arrays and, because of the language rules, you can do pretty much anything you can do to an array through pointers, but they are definitively not arrays.
- dnautics 6y agoI think we see the problem here. I've been using C on and off (granted, never formally educated in it) for 20 years now and I don't know the difference.
- coliveira 6y agoAn array is a variable that is declared to contain sequential data (using square brackets and the desired number of elements). It is immutable. A pointer is a mutable variable that can be made to point to anything, and it can be used to reference an array or any of its elements. These are different concepts, although the language allows a lot of operations in common between them.
- dnautics 6y agoI see. I mean, yes I obviously know the difference between pointers and arrays as general CS concepts (and articulated how other languages get it correct), but we're not talking about CS, we're talking about C. Perhaps I should have said in my original context, "array syntax" is the same as "pointer syntax". But while the CS concepts of "array" and "pointer" are indeed different, the C syntax elides the two. C does not track the immutable length of the array for you.
- globular-toast 6y agoI think what you're getting at is that arrays are not really first-class objects in C. Rather it just gives you a bit of syntactic sugar to do the same thing that you could do with pointers anyway. But it's up to the programmer to complete the abstraction.
- coliveira 6y agoNo, I described how it is in C. C has a proper array syntax, although a poor one. The problem is that pointers can do a lot of the same, so people get confused.
- globular-toast 6y agoI do remember a time in my very early C career where I thought "pointers are arrays". But I'm not sure what caused me to think that. They are two very different things. A pointer is a memory address. An array is an area of contiguous memory containing a whole number multiple of some data type. Pointer arithmetic naturally supports arrays, but the pointer is not the array.
- wnissen 6y agoPointers are super hard if you've not dealt with indirection before. Especially in C/C++ when you can dereference pretty much anything and you get a runtime segfault with no other diagnostic. Do you not remember when that was mystifying? Given that a huge fraction of vulnerabilities are unsafe memory accesses, I think most people still make plenty of mistakes around pointers. The analogy I like is the address of IHOP (International House of Pancakes). If I know the address of IHOP is 4567 Portobello Rd., why don't I have pancakes?!? It's the same with a pointer. Similarly, if I have a pancake, and someone calls and asks me for the address of IHOP, it's also wrong to bring the pancake to them in lieu of the address.