Repository navigation
Expand file tree
/
Copy pathft_printf_pointer.c
More file actions
55 lines (49 loc) · 1.82 KB
/
Copy pathft_printf_pointer.c
File metadata and controls
55 lines (49 loc) · 1.82 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hutzig <hutzig@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 10:53:28 by hutzig #+# #+# */
/* Updated: 2024/05/13 17:55:43 by hutzig ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_ptr_hex(uintptr_t n, int total_length)
{
int cp;
cp = total_length;
if (n >= 16)
{
total_length = ft_ptr_hex(n / 16, total_length);
if (cp > total_length)
return (-1);
total_length = ft_ptr_hex(n % 16, total_length);
}
else if (n < 10)
total_length += ft_printf_char(n + 48);
else
total_length += ft_printf_char(n - 10 + 'a');
if (cp > total_length)
return (-1);
return (total_length);
}
/* The 'uintptr_t' is designed to hold pointer values (and hold any pointer to
* data without loss of information, so it's used to guaranteed to have the
* same size as a pointer */
int ft_printf_pointer(void *ptr)
{
uintptr_t address;
int total_length;
int prefix_len;
address = (uintptr_t) ptr;
prefix_len = ft_printf_string("0x");
if (prefix_len == -1)
return (-1);
total_length = ft_ptr_hex(address, 0);
if (total_length == -1)
return (-1);
total_length += prefix_len;
return (total_length);
}