I get the compiler error warning: cast from pointer to integer of different size.
Why is this
Because pointer and int
may have different length, for example, on 64-bit system, sizeof(void *)
(i.e. length of pointer) usually is 8, but sizeof(int)
usually is 4. In this case, if you cast a pointer to an int
and cast it back, you will get a invalid pointer instead of the original pointer.
and is there anyway I can cast the address pointed to by sbrk() to an int?
If you really need to cast a pointer to an integer, you should cast it to an intptr_t
or uintptr_t
, from <stdint.h>
.
From <stdint.h>(P)
:
- Integer types capable of holding object pointers
The following type designates a signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to a pointer to void, and the result will compare equal to the original pointer: intptr_t
The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to a pointer to void, and the result will compare equal to the original pointer: uintptr_t
On XSI-conformant systems, the intptr_t
and uintptr_t
types are required; otherwise, they are optional.