Repository navigation
Expand file tree
/
Copy pathprintptr.c
More file actions
75 lines (66 loc) · 1.76 KB
/
Copy pathprintptr.c
File metadata and controls
75 lines (66 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* :::::::: */
/* printptr.c :+: :+: */
/* +:+ */
/* By: jsmidt <jsmidt@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/10/27 15:56:01 by jsmidt #+# #+# */
/* Updated: 2025/10/31 13:38:10 by jsmidt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ilen(unsigned long long n)
{
int i;
i = 1;
while (n / 16)
{
n = n / 16;
i++;
}
return (i);
}
static char *ptr_itoahex(unsigned long long n)
{
unsigned long long m;
char *res;
int i;
m = n;
i = ilen(m);
res = malloc((i * sizeof(char) + 1));
if (!res)
return (NULL);
res[i] = '\0';
while (i)
{
if ((m % 16) < 10)
res[--i] = (m % 16) + '0';
if ((m % 16) >= 10)
res[--i] = (m % 16) + 'W';
m = m / 16;
}
return (res);
}
static int ptr_print_hexl(unsigned long long n)
{
int len;
char *str;
str = ptr_itoahex(n);
len = ft_strlen(str);
ft_putstr(str);
free(str);
return (len);
}
int ft_print_ptr(void *ptr)
{
unsigned long long address;
int count;
if (!ptr)
return (ft_putstr("(nil)"));
count = 0;
address = (unsigned long long)ptr;
count += ft_putstr("0x");
count += ptr_print_hexl(address);
return (count);
}