-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_pointer.c
71 lines (64 loc) · 1.69 KB
/
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
61
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cyferrei <cyferrei@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 17:37:15 by cyferrei #+# #+# */
/* Updated: 2023/11/23 13:30:22 by cyferrei ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ptr_len(unsigned long long nb)
{
int len;
len = 0;
while (nb != 0)
{
len++;
nb = nb / 16;
}
return (len);
}
void conv_ptr(unsigned long long nb)
{
if (nb >= 16)
{
conv_ptr(nb / 16);
conv_ptr(nb % 16);
}
else
{
if (nb <= 9)
ft_putchar_fd((nb + '0'), 1);
else
{
ft_putchar_fd((nb - 10 + 'a'), 1);
}
}
}
int print_pointer(unsigned long long ptr)
{
int final_len;
int ptr_lenght;
if (ptr == 0)
return (write(1, "(nil)", 5));
ptr_lenght = ptr_len(ptr);
final_len = 0;
final_len += write(1, "0x", 2);
if (ptr == 0)
final_len += write(1, "0", 1);
else
{
conv_ptr(ptr);
}
return (final_len + ptr_lenght);
}
/*int main(void)
{
unsigned long long test_ptr = 0; // ici votre valeur de test
printf("\nOutput of print_ptr: %d\n", print_ptr(test_ptr));
return (0);
}
*/