c strcpy strncpy
2016-07-04 16:29
246 查看
strcpy把从src地址开始且含有'\0'结束符的字符串复制到以dest开始的地址空间,返回值的类型为char*.
strncpy 拷贝数据,不包含最后的null-terminated.
//讨论优先级 * 和++ 是同一优先级但是,后加加时执行完语句后执行++操作
strncpy 拷贝数据,不包含最后的null-terminated.
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> using namespace std; int main() { const char* p = "hello world"; char des[128]; //memset(des, 0x00, sizeof(des)); /* Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. */ strncpy(des, p, 7);//不包含最后的null-terminated. cout << des << endl;//打印出乱码 return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char *src = "123 hello world"; char des[6] = { 0, }; //strncpy(des, src, strlen(src));//奔溃 strncpy(des, src, sizeof(des) - 1);//最多拷贝des的长度-1,最后一个放置 '\0' printf("%s\n",des); }
//讨论优先级 * 和++ 是同一优先级但是,后加加时执行完语句后执行++操作
//讨论优先级 * 和++ 是同一优先级但是,后加加时执行完语句后执行++操作 char* strcpy(char* des,const char* source) { char* r=des; assert((des != NULL) && (source != NULL)); while((*r++ = *source++)!='\0'); return des; }
相关文章推荐
- ionic学习记录(四)
- uiautomator日志转junit日志
- Spring MVC 系统异常处理方式及性能对比
- 潮词 乔贝穆凯
- 高效编写Java代码的几条建议
- Matlab中删除cell数组中的空元素
- redis队列的安装与配置,还有一些安全方面的配置
- https_request请求接口返回数据
- JSTL标签用法 详解
- Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数
- 【iOS】图片缩放动画
- C语言5种存储区域
- POJ 2288 Islands and Bridges
- Gradle
- android 防止 多次点击 重复点击按钮
- android中当editText设置了值之后,让游标跑到值的后面
- Robot Framework-用户关键字的使用方法
- KK GMS GoolgeCamera应用预置
- NTFS文件系统的简述
- Eclipse中Java开发中版权声明及编码风格的注释