Search code examples
c++cmemoryunsignedunsigned-integer

C/C++: How to properly convert "unsigned int *" value to "unsigned int"?


I have an "unsigned int *" value, and I need to convert it to a simple "unsigned int", so I could transfer it to a function. But, unfortunately, when I try to do a simple cast, the value gets changed:

Code:

unsigned int * addr;

...

fprintf(stdout, "=== addr: %08x ===\n", addr); fflush(stdout);
fprintf(stdout, "=== casted addr: %08x ===\n", (unsigned int)addr);


Output:

=== addr: fc880000 ===
=== casted addr: 400eff20 ===

Please tell me, how to convert this value properly, so it doesn't change during the conversion?


Solution

  • Simply use *addr. This is valid and should always work. In case you need to get the value of the pointer instead of the value pointed to by the pointer, you will need a larger type. Typically the value of an unsigned int* is 64 bit, while unsigned int is only 32 bit.