3 ms·
strncpy 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
by kazinator 10y ago
strncpy 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);