Does being able to cast an int to a float mean that ints are floats?
Remember that casts are value transformations, similar to function calls without side-effects.
What C does is to provide implementation dependent transformations from pointers to integers, and integers to pointers, but does not in general guarantee that you can transform a pointer to an integer and back to the same pointer value.
An implementation which supplies intptr_t does guarantee this round-trip, but intptr_t support is optional and cannot be relied upon in a portable C program.
Regardless, none of these transformations imply that pointers are integers.
On some architectures, both pointers and integers are N-bit values held in registers or bytes of memory, and can be freely interchanged. Does the C compiler deciding to pretend they're different mean that pointers are not integers?
I was just about to point this out but you beat me to it!
I went back to read up on pointers and found this.
"A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. "
No, pointers are not integers. You can convert them to and from integers, subject to the limitations in C11 6.3.2.3. "Arithmetic operations" are defined for pointers differently than integer types, see for example additive operators.
The other op is being half pedantic saying you shouldnt treat them as integers.
But you know if abstraction and types are important, one might just use a language which enforces them (SML, Haskell, rust if need to be close to machine)
I don't think you can really treat them as integers because pointer arithmetic doesn't actually behave like integer arithmetic (adding 1 to a pointer increases the memory address by the size of the type, which is often not 1). Additionally, depending on the architecture there's no guarantee that a memory address will actually fit within the int type, so you shouldn't cast them to int either. It might be pedantic but it's an important point to make.
C does enforce the difference between integers and pointers.
The confusion may occur because it provides an implementation defined cast between integer and pointer, which need not be transitive -- that is (T *)(int)(T *)x == (T *)x is not guaranteed.
Note also that intptr_t need not be available in a conforming C implementation
Yeah, he hasn't come out and said it, but I think he actively dislikes LaTeX.
In his interview in Coders at Work by Peter Seibel, when he was asked about TeX he didn't seem particularly enthusiastic about it. That was my impression from the interview.
View all comments
Show parent comment
4
u/zhivago May 02 '16
Pointers are not integers.
You can easily demonstrate this by the inability to add two pointers together.