3 ms·
You're correct about the problems, and I would always recommend strncpy over strcpy for safety, but it's not quite as limited as you portray: You only need to
by kd5bjo 10y ago
You're correct about the problems, and I would always recommend strncpy over strcpy for safety, but it's not quite as limited as you portray: You only need to ensure an upper bound on the string length that will fit into your destination buffer. In addition to literals, this can also be guaranteed by copying a string that's already stored in a container that's smaller than the target, such as a fixed-size buffer or database field.
- kazinator 10y agostrncpy is strcpy's daft cousin that wastefully writes extra null characters---except when there is no room; then it neglects to null-terminate at all. strncpy was not needed even in C90, because of sprintf, which is less clumsy to use, doing everything in one step: char array[64]; sprintf(array, "%.63s", some_string); If we replace the 63 with * , we can pass a dynamic size: sprintf(array, "%.*s", (int) sizeof array - 1, some_string);