问题
For example
printf("%u",&a);
gives me output
65524
which is a 16 bit address.
回答1:
Because you used wrong format specifier which invokes undefined behavior.
To print a pointer, you should use %p
format specifier and cast the argument to void*
. Something like
printf("%p",(void *)&a);
will do the job.
That said, you should look into the concepts of virtual memory the first thing.
回答2:
You can also simply answer your assumption about address size by checking the size of any pointer instead of a test variable's address:
printf("%zu\n", sizeof(int*));
printf("%zu\n", sizeof(float*));
Assuming that a byte in all systems equals eight bit, you can see about size of address.
Please see this SO post
回答3:
To find the highest possible address for (most of) the currently common systems do this:
#include <stdint.h>
#include <stdio.h>
int main(void)
{
uintptr_t uip = (uintptr_t) -1;
void * vp = (void*) uip;
printf("%p\n", vp);
}
来源:https://stackoverflow.com/questions/37256145/if-my-computer-is-a-32-bit-system-it-has-a-32-bit-address-right-but-when-i-pri