strcpy,strncpy http://blog.csdn.net/shutear/article/details/8256096
来源:互联网 发布:红苹果预算软件 编辑:程序博客网 时间:2024/04/30 10:14
http://blog.csdn.net/shutear/article/details/8256096
1、字符数组拷贝函数
常用的字符串拷贝函数为strcpy,strncpy,在高版本的中支持安全的拷贝函数:strcpy_s,strncpy_s。
2、函数原型
strcpy函数原型(一):
strcpy_s函数原型(一):
- char *strcpy(
- char *strDestination,//Destination string.
- const char *strSource //Null-terminated source string.
- );
3、举例
- errno_t strcpy_s(
- char *strDestination,//Location of destination string buffer
- size_t numberOfElements,//Size of the destination string buffer
- const char *strSource //Null-terminated source string buffer
- );
分别利用strcpy和strcpy_s的将二个字符串拼接到另一个字符串。
strcpy version:
- int main()
- {
- char* p1 = "Hello,";
- char* p2 = "world!";
- int p1Length = strlen(p1);
- int p2Length = strlen(p2);
- int totalLengthOfP1AndP2 = p1Length + p2Length;
- char * concatP1AndP2 = new char[totalLengthOfP1AndP2+1];//add one to store '\0'
- strcpy(concatP1AndP2,p1);
- strcpy(concatP1AndP2 + p1Length ,//dest start location:concatP1AndP2 add p1Length
- p2);//src
- cout<<"p1:"<<p1<<endl;
- cout<<"p2:"<<p2<<endl;
- cout<<"p1+p2:"<<concatP1AndP2<<endl;
- delete []concatP1AndP2;
- concatP1AndP2 = NULL;
- return 0;
- }
strcpy_s version:
- int main()
- {
- char* p1 = "Hello,";
- char* p2 = "world!";
- int p1Length = strlen(p1);
- int p2Length = strlen(p2);
- int totalLengthOfP1AndP2 = p1Length + p2Length;
- char * concatP1AndP2 = new char[totalLengthOfP1AndP2+1];//add one to store '\0'
- strcpy_s(concatP1AndP2,//dest
- p1Length + 1,//number of elements of dest to store src including '\0'
- p1);//src
- strcpy_s(concatP1AndP2 + p1Length,//dest start location:concatP1AndP2 add p1Length
- p2Length + 1,//number of elements of dest to store src including '\0'
- p2//src
- );
- cout<<"p1:"<<p1<<endl;
- cout<<"p2:"<<p2<<endl;
- cout<<"p1+p2:"<<concatP1AndP2<<endl;
- cout<<"********************************"<<endl;
- delete []concatP1AndP2;
- concatP1AndP2 = NULL;
- return 0;
- }
使用注意事项: strcpy_s中的第二个参数:numberOfElements为至少是src字符串的长度加1('\0'),若不满足此条件,会进行参数校验,提示目标字符串长度不足。同理,可以使用strncpy和strncpy_s。
4、参考
[1] strcpy
[2] strcpy_s
版权声明:本文为博主原创文章,未经博主允许不得转载。
0 0
- strcpy,strncpy http://blog.csdn.net/shutear/article/details/8256096
- http://blog.csdn.net/IBM_hoojo/article/details/5688947
- http://blog.csdn.net/chenlaic/article/details/6143235
- http://blog.csdn.net/eaglewood2005/article/details/4335052
- http://blog.csdn.net/mchp/article/details/3995970
- http://blog.csdn.net/v_july_v/article/details/6015165
- http://blog.csdn.net/masterz/article/details/6232585
- http://blog.csdn.net/perfectpdl/article/details/6442847
- http://blog.csdn.net/eroswang/article/details/1967243
- http://blog.csdn.net/zhvsby/article/details/5986645
- http://blog.csdn.net/dz45693/article/details/6183645
- http://blog.csdn.net/david_lv/article/details/5798003
- http://blog.csdn.net/zxingchao2009/article/details/6299313
- http://blog.csdn.net/zhanxinhang/article/details/6783766
- http://blog.csdn.net/ruanruoshi/article/details/935510
- http://blog.csdn.net/yming0221/article/details/6538527
- http://blog.csdn.net/yming0221/article/details/6528490
- http://blog.csdn.net/yming0221/article/details/6704079
- Vector::assign的使用
- iOS - 如何集成支付宝
- 若干有序序列,合并成一个新的有序序列
- Project Euler:Problem 91 Right triangles with integer coordinates
- 大型网站技术架构笔录(1):演化
- strcpy,strncpy http://blog.csdn.net/shutear/article/details/8256096
- 黑马程序员----JAVA基础----IO流_3及反射
- strspn函数和strbrk函数
- qt项目生成exe文件指南
- Cocos2d-x开发教程 第四节 深入Action
- 字符串拷贝和内存拷贝函数
- 78 Subsets
- J2EE常用的13个标准
- [MFC] MFC 字体设置