I'm working on the implementation of strdup()
in C, I'm new to C and I'm trying to build my own library. I'm using the actual strdup()
from string.h
to test mine and I run into a bus error when I test my source against a main program but none when I use the actual strdup()
. Also I can't use any existing functions excluding malloc()
.
Source code:
#include <stdlib.h>
char *ft_strdup(char *src)
{
char *str;
int len;
while (src[len])
len++;
str = (char*)malloc(sizeof(*str) * (len+1));
while (*src)
*str++ = *src++;
*str = '\0';
return (str);
}
Why do I keep getting a bus error?