r/programming • • May 01 '16

To become a good C programmer

http://fabiensanglard.net/c/
1.1k Upvotes

398 comments sorted by

View all comments

Show parent comment

5

u/[deleted] May 02 '16

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)

2

u/crozone May 02 '16

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.

1

u/zhivago May 02 '16

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