Function name : strncpy
Header file : <string.h>
Function prototype : char *strncpy(char *destin,const char *source,int n);
Function : Splice the specified number of source strings behind the target string
Parameters : char *destin is the target string
const char *source is the source string to be spliced
int n is the number of characters to be spliced
Return value : Returns a pointer to the string destin
Note : If there are valid characters in the target character array, n lengths will be overwritten. This function does not generate a new string, but modifies the original string. Therefore, destin can only be a character array, not a string pointed to by a string pointer, because the string pointer points to a string constant, and the constant cannot be modified.
Program example: Copy the first n characters of the string source to destin and output the result
#include<stdio.h>#include<string.h>intmain(void){char*source=www.dotcpp.comverymuch!;chardestin[30]={GoodLuck!};strncpy(destin,source,14);printf (%sn,destin);return0;}
Running results:
www.dotcpp.com