HN2new | past | comments | ask | show | jobs | submitlogin
Pointers are integers (alecbenzer.com)
2 points by alecbenzer on Sept 9, 2011 | hide | past | favorite | 2 comments


It's somewhat useful to think of integers this way but it can be dangerous. a lot of operations on integers are 'undefined' on pointers. for example integer subtraction (which is quite common for measuring size) is undefined if the pointers are pointing to different memory objects. same with addition, if you increment a pointer past the end of an array you can create an undefined operation and who knows what the compiler will do.

So pointers are integers but with a big asterix.


I think I briefly mentioned why pointers are not literally just ints (though I didn't go into pointer arithmetic), but I feel that it's easier to tell someone it's an integer and then explain the ways in which the compiler treats them differently. I've heard people, for example, ask things like "does this function return a pointer or just an address?" (when they meant to ask "does this function return a pointer or a reference to a pointer?"), and understanding pointers in that sense seems more prevalent when you're just learning them than understanding the technicalities of how your compiler handles things like pointer arithmetic.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: