-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strdup.c
More file actions
47 lines (43 loc) · 1.52 KB
/
ft_strdup.c
File metadata and controls
47 lines (43 loc) · 1.52 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lcosta-g <lcosta-g@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 13:09:14 by lcosta-g #+# #+# */
/* Updated: 2024/10/26 11:57:56 by lcosta-g ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
char *dup;
size_t i;
i = 0;
dup = (char *)malloc(ft_strlen(s) + 1);
if (!dup)
return (NULL);
while (s[i])
{
dup[i] = s[i];
i++;
}
dup[i] = '\0';
return (dup);
}
/*
#include <stdio.h>
int main(void)
{
char str[] = "strdup example";
printf("string address: %p\n", str);
printf("duplicated string address: %p\n", ft_strdup(str));
printf("duplicated string value: \"%s\"\n", ft_strdup(str));
printf("\n\n");
char str2[] = "";
printf("string address: %p\n", str);
printf("duplicated string address: %p\n", ft_strdup(str2));
printf("duplicated string value: \"%s\"\n", ft_strdup(str2));
}
*/