strncpy的用法

来源:互联网 发布:泡妞软件app 编辑:程序博客网 时间:2024/06/08 19:09
strncpy()属于strcpy()的扩展,使用时需要包含头文件#include <string.h>.
函数原型strncpy(char *s1,const char *s2,int n);其中有三个参数分别表示目标字符串s1,源字符串s2,拷贝长度。意思是将s2指向的字符串的前n个长度的字符放到s1指向的字符串中,并将s1原有的前n个字符覆盖.
例子:

#include<iostream>#include<cstring>using namespace std;int main(){    char s1[100];    char s2[100];    cin>>s1>>s2;    cout<<s1<<" "<<s2<<endl;    strncpy(s1,s2,3);    cout<<s1<<" "<<s2<<endl;    return 0;}

0 0