Context: Established programmer, revisiting C, not for any particular purpose other than better understanding what's really going on behind the scenes.
Consider this program.
#include <stdio.h>
int main()
{
//https://stackoverflow.com/questions/2581769/dereference-a-pointer-inside-a-structure-pointer
int concreteInteger = 42;
int* pointerInteger;
pointerInteger = &concreteInteger;
printf("concreteInteger as %%i = %i\n",concreteInteger);
printf("pointerInteger as %%p = %p\n" ,(void*)pointerInteger);
printf("pointerInteger as %%u = %u\n" ,(unsigned)pointerInteger);
printf("*pointerInteger as %%i = %i\n",*pointerInteger);
printf("Done\n");
return 0;
}
When I compile and run this program on OS X 10.11, I get the following output.
$ cc main.c;./a.out
concreteInteger as %i = 42
pointerInteger as %p = 0x7fff5d614878
pointerInteger as %u = 1566656632
*pointerInteger as %i = 42
Done
Where does 1566656632
come from? If I convert 1566656632
to hex I get 0x5D614878
, not 0x7fff5d614878
.
So where does 1566656632
come from? What incorrect assumption am I making above? Is casting a pointer as an unsigned number something that's undefined in C? If so, for bonus points, if I wanted to represent the hex address of a pointer as a number in base 10, what's the most straight forward way to do that?