I saw this code in project.
b
\'s type is void*
:
void *b = ...;
int a = (int) (unsigned long) b;
Is this line
This probably avoids a compiler warning on 64-bit Unix systems where unsigned long
is a 64-bit quantity and hence big enough to hold a pointer, but int
is a 32-bit quantity that is not big enough to hold a pointer. The cast to (unsigned long)
preserves all the bits of the address; the subsequent cast to int
throws away the high-order 32-bits of the address, but does so without getting a warning by default.
To demonstrate:
int main(void)
{
void *b = (void *)0x12345678;
int a = (int)(unsigned long)b;
int c = (int)b;
return a + c;
}
$ gcc -O3 -g -std=c99 -Wall -Wextra -c ar.c
ar.c: In function ‘main’:
ar.c:5:15: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
$
Using GCC 4.7.1 on Mac OS X 10.8.4, defaulting to 64-bit compilation.
It is interesting to speculate what will be done with the 'part of an address' value.