-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf_ptr.c
59 lines (52 loc) · 1.49 KB
/
printf_ptr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mheinke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 20:14:55 by mheinke #+# #+# */
/* Updated: 2023/09/12 12:28:05 by mheinke ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int size_hex_address(unsigned long long n)
{
int size;
size = 0;
while (n)
{
size ++;
n /= 16;
}
return (size);
}
static int ft_hex_address(unsigned long long n)
{
char *base_16;
int size;
size = size_hex_address(n);
base_16 = "0123456789abcdef";
if (n < 16)
{
if (printf_char(base_16[n]) == -1)
return (-1);
}
else
{
ft_hex_address(n / 16);
ft_hex_address(n % 16);
}
return (size);
}
int printf_ptr(void *ptr)
{
int n;
if (printf_string("0x") == -1)
return (-1);
n = ft_hex_address((unsigned long long) ptr);
if (n != 0)
return (n + 2);
else
return (3);
}