-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_pointer.c
82 lines (70 loc) · 1.26 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
72
73
74
75
76
77
78
79
80
81
82
#include "main.h"
/**
* print_hex_to_pointer - prints an hexgecimal number.
* @num: the argument of the print_hex_to_pointer function.
* Return: A total count of the characters printed.
*/
int print_hex_to_pointer(unsigned long int num)
{
long int *hex;
long int cntr = 0, i;
unsigned long int temp = num;
if (num == 0)
{
_putchar('0');
return (1);
}
while (num / 16 != 0)
{
num /= 16;
cntr++;
}
cntr++;
hex = malloc(sizeof(long int) * (cntr));
if (hex == NULL)
return (-1);
if (hex)
{
for (i = 0; i < cntr; i++)
{
hex[i] = temp % 16;
temp /= 16;
}
for (i = cntr - 1; i >= 0; i--)
{
if (hex[i] > 9)
hex[i] = hex[i] + 39;
_putchar(hex[i] + '0');
}
free(hex);
return (cntr);
}
return (0);
}
/**
* print_pointer - prints an hexgecimal number.
* @arg: the argument of the integer function..
* Return: A total count of the characters printed.
*/
int print_pointer(va_list arg)
{
void *ptr;
char *str = "(nil)";
unsigned long int val;
int print_hex;
int i;
ptr = va_arg(arg, void *);
if (ptr == NULL)
{
for (i = 0; str[i] != '\0'; i++)
{
_putchar(str[i]);
}
return (i);
}
val = (unsigned long int)ptr;
_putchar('0');
_putchar('x');
print_hex = print_hex_to_pointer(val);
return (print_hex + 2);
}