字符串拷贝函数strcpy写法_转
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// CppReference.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
using namespace std; /*
* 说明:字符串拷贝版本1
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错或者有重叠,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
*/
char *strcpy_v1(char *dest , const char *src)
{
//调试时,使用断言,入口检测
assert( (dest!=NULL) && (src!=NULL) ); //注意这里的内存指向参数dest所在的内存,不是栈内存,因而可以在函数中返回
char *to = dest; //主要操作在while条件中完成
while( (*dest++ = *src++)!='\0')
{
NULL;
} //返回拷贝字符串首地址,方便连缀,比如strlen(strcpy(dest,"hello"))
return to;
} /*
* 说明:字符串拷贝版本2
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
*/
char *strcpy_v2(char *dest , const char *src)
{
char *d = dest;
char c; while((c=*src++) != '\0')
{
*(dest++)=c;
} *dest='\0'; return d;
} /*
* 说明:字符串拷贝版本2(你能找出错误的原因吗)
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
*/
char *strcpy_v2_error(char *dest , const char *src)
{
char *d = dest;
char c; while((c=*src++) != '\0')
{
*(d++)=c;
} *d='\0'; return d;
} /*
* 说明:字符串拷贝版本3
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
*/
char *strcpy_v3(char *dest , const char *src)
{
char *d = dest;
char c; while(*src)
*dest++ = *src++; *dest='\0'; return d;
} /*
* 说明:字符串拷贝版本4
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。
*/
char *strcpy_v4(char *dest , const char *src)
{
char *d = dest;
char c; while( (*dest = *src)!='\0')
{
src++;
dest++;
} *dest='\0'; return d;
} /*
* 说明:字符串拷贝版本5
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。restrict关键字限定字符串不能重叠。
*/
char *strcpy_v5(char* _restrict dest , const char* _restrict src)
{
char *d = dest;
char c; while( (*dest = *src)!='\0')
{
src++;
dest++;
} *dest='\0'; return d;
} /*
* 说明:字符串拷贝版本6
* 参数:dest目标地址,src源地址
* 返回:返回拷贝好的地址;如果出错,无定义
* 异常:可能出现字符串溢出,及dest所占空间不如src所占空间大。restrict关键字限定字符串不能重叠。
*/
char *strcpy_v6(char* _restrict dest , const char* _restrict src)
{
char *d = dest;
char c; while(*dest++=*src++);
return d;
} int _tmain(int argc, _TCHAR* argv[])
{
char buf[]; char *buf2 = (char *)calloc(,sizeof(char)); char *buf3 = (char *)malloc(*sizeof(char)); char *buf5 = (char *)malloc(*sizeof(char)); char *buf6 = (char *)malloc(*sizeof(char)); printf("using strcpy_v1,the string 'Hello,World'\'s length is : %d\n",strlen(strcpy_v1(buf,"Hello,World"))); printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2(buf2,"This is the best age"))); printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2_error(buf2,"This is the best age"))); printf("using strcpy_v3,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v3(buf3,"This is the best age"))); printf("using strcpy_v5,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v5(buf5,"This is the best age"))); printf("using strcpy_v6,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v6(buf6,"This is the best age"))); system("pause"); return ;
}
转自:http://www.cnblogs.com/zxher/archive/2010/07/20/1781209.html
字符串拷贝函数strcpy写法_转的更多相关文章
- C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点
1 首先介绍几个常用到的转义符 (1) 换行符“\n”, ASCII值为10: (2) 回车符“\r”, ASCII值为13: (3) 水平制表符“\t”, ASCII值为 9 ...
- 编写实现字符串拷贝函数strcpy()完整版
有个题目编程实现字符串拷贝函数strcpy(),很多人往往很快就写出下面这个代码. void strcpy( char *strDest,char *strSrc ) { while(( *strDe ...
- C语言中的字符串拷贝函数strcpy和内存拷贝函数memcpy的区别与实现
strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符'\0'. 已知st ...
- 20140902 字符串拷贝函数 右旋转字符串 string类的编写
1.strncpy字符串拷贝函数 //strncpy的程序 #include<stdio.h> #include<assert.h> char *strncpy1(char * ...
- 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...
- 字符串拷贝函数递归与非递归的C语言实现
初学递归的时候,觉得很抽象,不好分析,确实如此,尤其是有些时候控制语句不对,导致程序进去无限次的调用,更严重的是栈溢出.既要正确的控制结束语句,又要有正确的进入下次递归的语句,还要有些操作语句.... ...
- C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...
- 嵌入式-C语言基础:实现字符串拷贝函数
自己实现一个字符串的拷贝函数 #include<stdio.h> #include<stdlib.h> #include <string.h> char * mys ...
- c语言的字符串拷贝函数的精简
#include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){ while ((*to ...
随机推荐
- System-Defined Device Setup Classes Available to Vendors
https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx
- 7. Add a networking service
Controller Node: 1. sudo vi /etc/nova/nova.conf [DEFAULT] ... network_api_class = nova.network.api.A ...
- 【液晶模块系列基础视频】3.3fatfs接口函数的使用3
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- Redis Sentinel哨兵集群
Redis Sentinel(哨兵集群)是一种高可用的redis部署方案.在集群中的redis-master服务挂掉时,无需人为干预,即可通过哨兵集群的自我调整,实现redis服务的持续可用. 哨兵集 ...
- dp和px的转换
/** * dp转px * @param context * @param dp * @return */ public static int dp2px(Context context, float ...
- swift 子类继承父类
// 子类的指定构造方法必须调用父类构造方法,并确保调用发生在子类存储属性初始化之后.而且指定构造方法不能调用同一个类中的其他指定构造方法: // 便利构造方法必须调用同一个类中的其他指定构造方法(可 ...
- System.Data.OracleClient.OracleConnection已过时
解决办法如下: 1.把原来的using System.Data.OracleClient;去掉 2.在oracle安装目录下找到Oracle.DataAccess.dll 添加引用:using Ora ...
- PHP5.4安装xhprof扩展[不要去pecl下载]
HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装. 但pecl上的版本不支持PHP5.4 可以到github上的xhprof库中下载:https:/ ...
- px_ipc_name.c
/* include px_ipc_name */ #include "unpipc.h" char * px_ipc_name(const char *name) { char ...
- java TimeUnit synchronized demo
import java.util.concurrent.TimeUnit; public class TestCalc { private static boolean stopRequested=f ...