top | item 10781571

(no title)

sepeth | 10 years ago

It puts zeros to unused bytes in the destination. Here's a note from its man page:

Some programmers consider strncpy() to be inefficient and error prone. If the programmer knows (i.e., includes code to test!) that the size of dest is greater than the length of src, then strcpy() can be used.

One valid (and intended) use of strncpy() is to copy a C string to a fixed-length buffer while ensuring both that the buffer is not overflowed and that unused bytes in the target buffer are zeroed out.

discuss

order

halayli|10 years ago

You're talking about strncpy not strncat.

sepeth|10 years ago

Ah sorry, I was misreading it from the beginning :(