Something went wrong. Try again.
My solution for the ft_printf assignment.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283/* ************************************************************************** *//* *//* ::: :::::::: *//* ft_putptr.c :+: :+: :+: *//* +:+ +:+ +:+ *//* By: gvoelkne <marvin@42.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* Created: 2026/05/01 11:36:45 by gvoelkne #+# #+# *//* Updated: 2026/05/06 16:54:08 by gvoelkne ### ########.fr *//* *//* ************************************************************************** */
#include "ft_printf.h"
static unsigned int calc_digits(uintptr_t num, int base){ int count;
count = 0; if (num == 0) return (1); while (num > 0) { count += 1; num /= base; } return (count);}
static char *ptr_to_hex(uintptr_t ptr){ int curr; char *res; int res_size;
if (ptr == 0) return (ft_strdup("0x0")); curr = 0; res_size = calc_digits(ptr, 16) + 3; res = ft_calloc((res_size + 1), sizeof(char)); if (!res) return (NULL); res[res_size--] = '\0'; while (ptr > 0) { if ((ptr % 16) > 9) res[(res_size - 1)] = ((ptr % 16) - 10) + 'a'; else res[(res_size - 1)] = (ptr % 16) + '0'; ptr /= 16; res_size -= 1; } res[0] = '0'; res[1] = 'x'; return (res);}
int ft_putptr(void *ptr){ char *s; ssize_t write_count; int is_nil;
is_nil = 0; if (!ptr) { s = "(nil)"; is_nil = 1; } else { s = ptr_to_hex((uintptr_t)ptr); if (!s) return (0); } write_count = write(1, s, ft_strlen(s)); if (write_count < 0) return (0); if (!is_nil) free(s); return (write_count);}