Umm . . . no.
Code:
$ cat strncpy.c
#include <stdio.h>
#include <string.h>
int main() {
const char *src = "something";
char dest[4];
size_t x;
strncpy(dest, src, 4);
for(x = 0; x < sizeof dest; x ++) {
printf("'%c' [%d]\n", dest[x], dest[x]);
}
return 0;
}
$ gcc strncpy.c -o strncpy
$ ./strncpy
's' [115]
'o' [111]
'm' [109]
'e' [101]
$
Voilà. Non-NULL-terminated.
[edit] In your last example, the result may be "som\0", but that's only because you passed "sizeof space - 1". You're relying on space[3] to be '\0', which you haven't set it to. (Plus you misspelled "strncat". )
Thinking about it a bit, the most efficient way to do this would be like this.
Code:
char space[4];
space[sizeof space - 1] = 0;
strncpy(space, "something", sizeof space - 1);
That way the strncpy() never overwrites the last character. Or perhaps . . .
Code:
char space[4];
const char *source = "something";
size_t len;
len = strlen(source);
if(len >= sizeof space) {
space[sizeof space - 1] = 0;
}
strncpy(space, source, sizeof space - 1);
[/edit]