ft_strdup
Subject
STRDUP(3) (simplified)
NAME
strdup -- save a copy of a string
SYNOPSIS
char *strdup(const char *s1);
DESCRIPTION
The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3).
If insufficient memory is available, NULL is returned and errno is set to ENOMEM.
Understandable explanation
For once, the man is really clear on what the function does. So I don't think I need to explain it with more details.
Hints
We have to use malloc for this since the returned value of this function must be 'freeable' with the free function.
char *ft_strdup(const char *s1)
{
/* use malloc to allocate enough space for s1
* we will have to copy it completely
* so we need enough space for it
*/
/* loop over s1 and copy each character in the new string you
* just allocated
*/
/* return the allocated and copied string */
}
Commented solution
Last updated
Was this helpful?