Strlen
A standard C function that returns the length of a string.
Strncpy
A C standard library function: strncpy is used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings
Source first bytes
Example |
---|
"This is the case here because strlen hello is greater than 3 your last strncpy argument" from question Access violation writing to static global variable? |
"However if strlen source is greater than n then strncpy will simply copy the first n bytes and will not terminate the string dest with a null byte because there is no space for it" from question Do I need to assign \0 to malloc strings in c? |
Others
Example |
---|
Your loop is safe because it is based on strlen not the number of substr you computed;strncpy does not null terminate strings from question Why is strncpy in my for loop uneffective? |
For example strlen returns the number of characters excluding the terminating null so buffers need to be one byte larger than what it returns to hold things;also strncpy does not guarantee null termination while snprintf does from question How to assign string value in C from void *? |
Instead pass strncpy the size of the buffer or one more than the strlen str from question Why is valgrind reporting an uninitialised value error? |