/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/11/14 21:11:42 by hulamy #+# #+# */ /* Updated: 2019/11/20 16:39:29 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ /* ** create a new list */ /* ** #include ** ** void *ft_memcpy(void *dst, const void *src, size_t n) ** { ** size_t i; ** char *ptr; ** char *ptr2; ** ** ptr = (char *)dst; ** ptr2 = (char *)src; ** i = -1; ** while (++i < n) ** ptr[i] = ptr2[i]; ** return (dst); ** } ** ** typedef struct s_list ** { ** void *content; ** struct s_list *next; ** } t_list; ** ** t_list *ft_lstnew(void *content); ** ** int main(void) ** { ** char tresor; ** t_list *toto; ** ** tresor = 'd'; ** printf("tresor : %c\n",tresor); ** toto = ft_lstnew(&tresor); ** //toto->content was alocated as void* so it need cast ** printf("toto->content : %c\n",*(char*)(toto->content)); ** tresor = 'D'; ** printf("transform tresor : %c\n",tresor); ** printf("but not toto->content: %c\n",*(char*)(toto->content)); ** return (0); ** } */ #include "libft.h" t_list *ft_lstnew(void *content) { t_list *lst; if (!(lst = (t_list *)malloc(sizeof(*lst)))) return (NULL); if (!content) lst->content = NULL; else { if (!(lst->content = malloc(sizeof(content)))) return (NULL); ft_memcpy(lst->content, content, sizeof(content)); } lst->next = NULL; return (lst); }