strncpy copies 'n' bytes from one string to another.
Library: string.h
Prototype: char strncpy(char s1, const char *s2, size_t n);
Syntax: size_t n;
char string1[20]="red dwarf";
char string2[20]=";
strncpy(string2, string1, 4);
Notes
-
It may be nessacary to add your own NULL
terminator. see strcpy for more information. strncpy
has serious flaws.
-
It doesn't put a nul-terminator on the destination if it is completely filled;
and
-
It always completely fills the destination, with nuls, which is unnecessary.
example
program.
An
alternative method using 'sprintf'...
See also:
Martin Leslie