Reputation: 19
I've made function that prints memory address to the stdout but it dosent work the sameway as printf("%p", ...). For example the printf gives me this: 0x7fff58a2d2bb but my function prints something like this: 58a2d2bb. I know what 0x mean but cant get what 7fff mean. Can someone explain me what does that part mean and how can I add it to my code:
char *ft_itoa_base(uintmax_t num, uintmax_t base){
int i;
uintmax_t val_cp;
uintmax_t rem;
char *str;
val_cp = num;
i = 1;
while((val_cp /= base) >= 1)
i++;
str = ft_strnew(i); // Basically what it does (char*)malloc(sizeof(char) * (i + 1))
str[i] = '\0';
while(i-- > 0)
{
rem = num % base;
str[i] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num /= base;
}
return (str);}
char a = 'x';
void* p0 = &a;
uintmax_t i = (uintmax_t)p0;
ft_putstr(ft_itoa_base(i, 16));
ft_putchar('\n');
printf("PrintF: %p", p0);
Upvotes: 1
Views: 2829
Reputation: 153547
What does 0x7fff mean?
The missing "0x7fff" is part of the correct hexadecimal address lost in errant code of ft_itoa_base()
.
OP's original ft_itoa_base()
was not fully uintmax_t
ready given one of the variables was int instead of uintmax_t.
That caused the output to print a truncated address of "58a2d2bb" rather than the correct "7fff58a2d2bb".
The posted corrected ft_itoa_base()
has minor weaknesses.
// char* ft_itoa_base(uintmax_t num, uintmax_t base) {
// No need for base to be so wide with uintmax_t
char* ft_itoa_base(uintmax_t num, int /* or unsigned */ base) {
int i;
uintmax_t val_cp;
// uintmax_t rem;
// No need for rem to be so wide with uintmax_t
int /* or unsigned */ rem;
char *str;
...
Upvotes: 2
Reputation: 1330
0x7fff58a2d2bb
is 140,734,680,453,819
expressed in hexadecimal (or base 16) format where the digit a
represents 10, b
represents 11, and up to f
which represents 15.
Base 16 or hexadecimal format is preferred over base 10 for memory addresses because 16 is a power of 2 which makes it handy for viewing as bit masks, and 10 is not which makes it difficult to view in terms of bit masks.
Upvotes: 2