From 217bcb0d4e3ba60604921cb40d5a11a64f93cfc7 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 15 Jul 2019 10:20:37 +0200 Subject: c08 strdup malloc + 1 for \0, c09 begin --- c08/ex04/ft_strs_to_tab.c | 8 ++++---- c08/main.c | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'c08') diff --git a/c08/ex04/ft_strs_to_tab.c b/c08/ex04/ft_strs_to_tab.c index f6fbfaf..51ca0af 100644 --- a/c08/ex04/ft_strs_to_tab.c +++ b/c08/ex04/ft_strs_to_tab.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 17:15:32 by cacharle #+# #+# */ -/* Updated: 2019/07/14 11:12:08 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:12:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,12 @@ int ft_strlen(char *str) return (counter); } -char *ft_strdup(char *src) +char *ft_strndup(char *src) { int i; char *dup_ptr; - dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src)); + dup_ptr = (char*)malloc(sizeof(char) * ft_strlen(src) + 1); if (dup_ptr == NULL) return (NULL); i = 0; @@ -54,7 +54,7 @@ struct s_stock_str *ft_strs_to_tab(int ac, char **av) { tmp_stock.size = ft_strlen(av[i]); tmp_stock.str = av[i]; - if ((tmp_stock.copy = ft_strdup(av[i])) == NULL) + if ((tmp_stock.copy = ft_strndup(av[i])) == NULL) return (NULL); strs_stocks[i++] = tmp_stock; } diff --git a/c08/main.c b/c08/main.c index 67a40dc..c2a6dda 100644 --- a/c08/main.c +++ b/c08/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/12 15:24:19 by cacharle #+# #+# */ -/* Updated: 2019/07/13 08:55:06 by cacharle ### ########.fr */ +/* Updated: 2019/07/15 10:11:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,15 +27,15 @@ int main() printf("--------------------\n"); char **a = (char**)malloc(sizeof(char*) * 3); - a[0] = (char*)malloc(sizeof(char) * 32); - a[1] = (char*)malloc(sizeof(char) * 32); - a[2] = (char*)malloc(sizeof(char) * 32); - strcpy(a[0], "binjour"); - strcpy(a[1], "je"); - strcpy(a[2], "suis"); + a[0] = (char*)malloc(sizeof(char) * 1024); + a[1] = (char*)malloc(sizeof(char) * 1024); + a[2] = (char*)malloc(sizeof(char) * 1024); + strcpy(a[0], "ieSrBXBI5d4rW9O2"); + strcpy(a[1], "eadLhPjehRMwjqv9JJ7Q0WiLTBoVxsLx"); + strcpy(a[2], "MKynFC5rJwCJ0jdD"); t_stock_str *b = ft_strs_to_tab(3, a); for (int i = 0; b[i].str != 0; i++) - printf("%s | %s | %d\n", b[i].str, b[i].copy, b[i].size); + printf("%s \n%s | %d\n", b[i].str, b[i].copy, b[i].size); b[0].copy = "bafj"; ft_show_tab(b); -- cgit