2

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

Martin Naughton
  • 1,476
  • 17
  • 14
  • 1
    "*the C code automatically get the value in the address pointed at by the pointer and inserts it in to the integer*" - That's what you'd get by *dereferencing* the pointer, i.e. `int castNumber = *pnumber;`. – Oliver Charlesworth Aug 30 '15 at 12:29
  • http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p6 But you seem to have a missconception about pointers, addresses, values, dereferencing, etc. (no offence!). I'd recommend to read a C book or try a different one. – too honest for this site Aug 30 '15 at 12:30
  • related: [Is it safe to cast pointer to int and later back to pointer again?](http://stackoverflow.com/q/3567905/33499) – wimh Aug 30 '15 at 12:31

2 Answers2

-1

Casting actually says to the conpiler ''take a look at the value here and treat it as another type''. In case of pointer, the address which is kept by pointer becomes now an integer. And yes, in 64 bit mode you cannot store sn address as integer but as long int.

Example:

int k=3;
int *pInt = &k;
long int castPtr = (long int)pInt;
int *another_pInt = (int*)castPtr; // *another_pInt is now 3

EDIT:

Prefer unsigned long over long int. You probably should not care about the sign of the value since it ia an address.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • Why negative? I actually work a lot with pointers casted to 8byte unsigned integers. It is much easier to manipulate and calculate the values in case of some firmware implementation which actually manages the memory. Please explain your negative voting. What is incorrect in what I wrote? – Alex Lop. Aug 30 '15 at 13:15
-2

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.

Offcourse it is not taking the value that the pointer is pointing at.

To get the value that the pointer is pointing at, you need to do *pnumber(dereference). Using only pnumber you will get the address to which the pointer is pointing to.

When you store a pointer address in an integer data type, the result would depend on cases.

1) If the integer data type and the length of the address are different, then the extra bytes of the address would be lost when storing it in a variable of integer data type.

2) In cases when the size is same, you can store the address in an integer data type. But, i cannot think of a situation when you would need to.

Another point is, if the size of the integer data type and the address is same, you can convert to int and back to the address safely. Read this, See this.

Community
  • 1
  • 1
Haris
  • 12,120
  • 6
  • 43
  • 70