Casting from a pointer to an integer. What exactly happens? Here are some of the ways i think it could happen:
- the C code automatically get the value in the address pointed at by the pointer and inserts it in to the integer.
- integer take the address in the pointer and stores the address in the 4 bytes.
Reason i am asking is because changing from 32 bit to 64 bit a pointer can not be cast to a integer any more. That is because the size of memory a pointer takes is 64 bit which is 8 bytes.
A pointer could be pointing to an integer or a long since we do not know the value size the pointer is pointing to.
Is it the pointer address can not be stored in the integer or the value it contains can not be stored in the integer?
Say this simple code. This will not work on 64bit. Even though the pointer is to be treated as an integer it can not be cast. This leads me to believe that it is not taking the value but the address of the pointer and inserting it in to the integer.
In 32 bit the integer takes the pointer address and knows that it has to take 4 bytes from that pointer address to get the full value.
Is that statement correct?
int main(void){
int number = 3;
int * pnumber = &number;
int castNumber = (int)pnumber;
}
I had a look at this question but still did not make sense what is actually happening under the hood. http://c-faq.com/ptrs/int2ptr.html