-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_pointer.c
More file actions
51 lines (45 loc) · 1.59 KB
/
Copy pathprint_pointer.c
File metadata and controls
51 lines (45 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amtan <amtan@student.42singapore.sg> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/04 16:30:21 by amtan #+# #+# */
/* Updated: 2025/12/05 19:35:13 by amtan ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_hex_uintptr_recur(uintptr_t n, size_t *printed);
int print_pointer(uintptr_t addr, size_t *printed)
{
int tmp;
int ret;
if (addr == 0)
return (print_str("(nil)", printed));
tmp = print_str("0x", printed);
if (tmp < 0)
return (-1);
ret = print_hex_uintptr_recur(addr, printed);
if (ret < 0)
return (-1);
return (ret + tmp);
}
static int print_hex_uintptr_recur(uintptr_t n, size_t *printed)
{
int ret;
int tmp;
char *symbols;
ret = 0;
symbols = "0123456789abcdef";
if (n >= (uintptr_t)16)
{
ret = print_hex_uintptr_recur(n / 16, printed);
if (ret < 0)
return (-1);
}
tmp = print_char(symbols[n % 16], printed);
if (tmp < 0)
return (-1);
return (ret + tmp);
}