-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_pointer.c
More file actions
46 lines (41 loc) · 850 Bytes
/
Copy pathprint_pointer.c
File metadata and controls
46 lines (41 loc) · 850 Bytes
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
#include "main.h"
/**
* print_pointer - This function prints the address a pointer points to.
* @ptr: The argument to print
* Return: An integer
*/
int print_pointer(void *ptr)
{
/* Cast the pointer to an unsigned long */
unsigned long address = (unsigned long)ptr;
int count = 0;
int i;
int hex_digit;
/* Print the address in hexadecimal format */
collab_putchar('0');
collab_putchar('x');
if (ptr != NULL)
{
/* Print the hexadecimal digits of the address */
for (i = (sizeof(ptr) * 2 - 1) * 4; i >= 0; i -= 4)
{
hex_digit = (address >> i) & 0xF;
if (hex_digit < 10)
{
collab_putchar(hex_digit + '0');
}
else
{
collab_putchar(hex_digit + 'a' - 10);
}
count++;
}
/* If the address was zero, print a '0'*/
if (count == 0)
{
collab_putchar('0');
count++;
}
}
return (count);
}