Re:C Strings and my slow descent to madness
by cellularmitosis on Friday April 07, 2023 @11:41AM
(#35482817)
Yeah, I was a little confused by the strncpy "gotcha": "The answer is that the destination gets filled with all the characters of the source string with no room left for the null terminator."
Well, I mean, the docs specifically call that out: "If src is less than len characters long, the remainder of dst is filled with ‘\0’ characters. Otherwise, dst is not terminated."
So if you want null termination in all cases, you need to pass len-1, not len.
Well, I mean, the docs specifically call that out: "If src is less than len characters long, the remainder of dst is filled with ‘\0’ characters. Otherwise, dst is not terminated."
So if you want null termination in all cases, you need to pass len-1, not len.