C/C++ 字符串拷贝函数
来源:互联网 发布:程序员mac 编辑:程序博客网 时间:2024/05/22 14:36
最近在看一些C++的资料~~看到有讲拷贝函数的写法,觉得听好玩儿的就写了出来,当作记录分享出来。
- 函数内用临时变量来接受形参,使得原有形参不受函数内部的改变。(不要轻易改掉形参的值)
- 注意函数内部的指针判空。
#include <iostream>using namespace std;char *MyStrCpy1(char *strDest, const char *strSrc){ char *tempStrDest = strDest ;//保存首地址 const char *tempStrSrc = strSrc ;//保存首地址 if ( tempStrDest == NULL || tempStrSrc == NULL) { return NULL ; } if ( tempStrDest == tempStrSrc) { return tempStrDest ; } // 一个一个拷贝 最后补充'\0' for(; *tempStrSrc!='\0'; ++tempStrDest,++tempStrSrc) { *tempStrDest = *tempStrSrc; } *tempStrDest='\0'; return strDest ;}char *MyStrCpy2(char *strDest, const char *strSrc){ char *tempStrDest = strDest ;//保存首地址 const char *tempStrSrc = strSrc ;//保存首地址 if ( tempStrDest == NULL || tempStrSrc == NULL) { return NULL ; } if ( tempStrDest == tempStrSrc) { return tempStrDest ; } //判断是否是‘\0’ while( (*tempStrDest++ = *tempStrSrc++) !='\0') ; return strDest ;}char *MyStrCpy3(char *strDest, const char *strSrc){ char *tempStrDest = strDest ;//保存首地址 const char *tempStrSrc = strSrc ;//保存首地址 if ( tempStrDest == NULL || tempStrSrc == NULL) { return NULL ; } if ( tempStrDest == tempStrSrc) { return tempStrDest ; } //直接判断这个条件是否为真 while( (*tempStrDest++ = *tempStrSrc++)) ; return strDest ;}int main(){ char myString[]="HELLO CSDN ,人间词话·CODE"; char temp1[100]; char temp2[100]; char temp3[100]; cout<<MyStrCpy1(temp1,myString)<<endl; cout<<MyStrCpy2(temp2,myString)<<endl; cout<<MyStrCpy3(temp3,myString)<<endl;}
阅读全文
0 0
- Linux c字符串拷贝函数
- C:strcpy字符串拷贝函数
- C/C++ 字符串拷贝函数
- C实现内存拷贝以及字符串拷贝函数
- C语言字符串拷贝
- C语言字符串拷贝
- C 字符串拷贝
- 【C语言】 字符串操作函数及内存拷贝函数归总
- 【C++】拷贝构造函数
- C++:拷贝构造函数
- c++"拷贝构造函数
- C++: 拷贝构造函数
- 【C++】拷贝构造函数
- c++::拷贝构造函数
- 【C++】:拷贝构造函数
- 【C++】拷贝构造函数
- C++:拷贝构造函数
- C语言 字符串常用函数(计算长度,拷贝,拼接,比较)
- 2017-6-16·······························································VNC实例
- 值得推荐的C/C++框架和库 (真的很强大)
- js/javascript每隔一段时间局部刷新部分代码
- HTTP笔记
- java基础语法
- C/C++ 字符串拷贝函数
- TRUNCATE、DELETE、DROP的区别
- ssm项目初试总结
- 设计模式—访问者模式(二十三)
- jquery/javascript/js点击弹出对话框
- c#学习(一)
- mybatis工作总结001_mybatis中_parameter使用和常用sql
- JVM(7):JVM调优-工具篇
- Swift 4 和 Objective-C 在同一个工程里的混搭编程的方法