3 ms·
You rarely want to use strncpy. Keep in mind: 1) char buf[1024]; strncpy(buf, "hello", sizeof buf); now fills in 5 characters, and 1019 0 bytes in the bu
by noselasd 12y ago
You rarely want to use strncpy. Keep in mind:
1)
char buf[1024]; strncpy(buf, "hello", sizeof buf);
now fills in 5 characters, and 1019 0 bytes in the
buffer. (i.e. you're writing 1018 nul bytes that's
usually not needed)
2)
char buf[5]; strncpy(buf, "hello", sizeof buf);
results in buf NOT being a string. (it isn't nul terminated). So with most use of strncpy, you'll have to also do
buf[sizeof buf - 1] = 0;