I've written a program which runs fine, but I noticed something which worries me a little bit. Here is a simplified snippet:
strlen returns the length of the string without the null termination. So I would say you have to doCode:char* src = "HELLO" ; int len = strlent(src) ; char* dest = malloc( len * sizeof(char) ) ; strncpy(dest, src, len) ; printf("src=%s, dest=%s\n", src, dest) ;
Is this a correct statement ?Code:.... char* dest = malloc( len * sizeof(char) + 1 ) ; strncpy(dest, src, len + 1) ; // so it copies the null terminator too
If so, why doesn't this cause any runtime errors in my program ?