|
|
|
|
|
by dllthomas
4515 days ago
|
|
"The n in strncpy describes to what size the destination buffer (not string) should be padded with '\0'." This is false. From the man page: "The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated." And the following code prints foo:ar on my system. char buffer[10];
strcpy(buffer, "foobar");
strcpy(buffer, "foo");
printf("%s:%s\n", buffer, buffer+4);
Edited to add: huh, scratch that. Obvious error in above test :-P. Testing it with strncat like I had meant to, it seems it is in fact padded, not just (possibly) terminated. Interesting, and very worth knowing if you are trying to move a probably small string to a large buffer under time pressure. |
|
Not sure what you want to say with the example code. Maybe swap it for strncpy and strlcpy and see whether that matches your expectations?