I am practicing on C programming language. I had Win 10 systems and I was practicing on it and I always get addresses which are multiples of 2. Know, I am practicing on Macbook Pro M3 and I get 9 digit addresses. So, nine is odd.
Also, here is the code and result:
#include <stdio.h>
int main(void)
{
int m = 10, n, o, *z;
z = &m;
printf("z stores the address of m = %p\n", z);
printf("*z stores the value of m = %d\n", *z);
printf("&m is the address of m = %p\n", &m);
printf("&n stores the address of n = %p\n", &n);
printf("&o stores the address of o = %p\n", &o);
printf("&z stores the address of z = %p\n", &z);
return 0;
}
z stores the address of m = 0x16fa13228
*z stores the value of m = 10
&m is the address of m = 0x16fa13228
&n stores the address of n = 0x16fa13224
&o stores the address of o = 0x16fa13220
&z stores the address of z = 0x16fa13218
I researched about it however I couldn't get good answers. Here what I found:
why the value of address in c s always even?
Strange address of a C variable in Mac OS X
I was expecting to see addresses in even digits.
From the C23 draft:
7.23.6.1 The fprintf
function
- The conversion specifiers and their meanings are:
p
The argument shall be a pointer to void or a pointer to a character type. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
That means that different implementations may produce different output, even if the actual pointer values would happen to be the same.
I was expecting to see addresses in even digits.
I'm assuming this means that you expected to see an even number of digits. Typically, no more than the significant non-zero digits will be printed so if you get 9 digits on your Mac, that's just an effect of that. A 32 bit address like 0x00000FFF
will often be printed as just 0xFFF
.
If you want the same format on all platforms and implementations, you could cast the pointer to uintptr_t
and print that instead.
Example:
#include <inttypes.h> // PRIxPTR
#include <stdint.h> // uintptr_t
//...
printf("&n stores the address of n = 0x%016" PRIxPTR "\n", (uintptr_t)&n);
Possible output:
&n stores the address of n = 0x000000016fa13224