-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_pointer.c
60 lines (55 loc) · 1.57 KB
/
ft_print_pointer.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mhuthmay <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 16:54:53 by mhuthmay #+# #+# */
/* Updated: 2024/09/19 17:08:53 by mhuthmay ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_print_hex_ptr(unsigned long num)
{
char *hex_digits;
char buffer[17];
int i;
int count;
hex_digits = "0123456789abcdef";
i = 16;
count = 0;
buffer[16] = '\0';
if (num == 0)
{
i--;
buffer[i] = '0';
}
while (num > 0)
{
i--;
buffer[i] = hex_digits[num % 16];
num /= 16;
}
ft_putstr_fd(&buffer[i], 1);
count = 16 - i;
return (count);
}
int ft_print_pointer(va_list argptr)
{
void *ptr;
unsigned long num;
int count;
ptr = va_arg(argptr, void *);
if (ptr == NULL)
{
ft_putstr_fd("(nil)", 1);
return (5);
}
num = (unsigned long)ptr;
count = 0;
ft_putstr_fd("0x", 1);
count += 2;
count += ft_print_hex_ptr(num);
return (count);
}