字符串拷贝函数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 ...
随机推荐
- $.each(),$.map()归纳
//$.each()对字典(没有索引).数组(有索引) 遍历 //两个参数 var json={"name":"李可","age":&quo ...
- php扩展redis,编译安装redis服务
首先安装redis扩展 https://github.com/phpredis/phpredis 下载http://redis.io/download 服务软件 cd到软件存放目录unzip phpr ...
- ST_SRID
定义 ST_SRID 以 ST_Geometry 对象作为输入参数,返回其空间参考 ID. 语法 sde.st_srid (g1 sde.st_geometry) 返回类型 整型 示例 创建下列表格: ...
- int左移32位的行为未定义/Coverity
int左移32位的行为未定义 Coverity 代码静态安全检测 Is Shifting more than 32 bits of a uint64_t integer on an x86 machi ...
- PHP 随机显示
<?php print_r( array_rand( array( "新春快乐"=>"", " ...
- GitHub超详细图文攻略
GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git 分类: 转载2014-03-25 21:10 10641人阅读 评论(2) 收藏 举报 GitHubbr ...
- Machine Learning in Action -- 回归
机器学习问题分为分类和回归问题 回归问题,就是预测连续型数值,而不像分类问题,是预测离散的类别 至于这类问题为何称为回归regression,应该就是约定俗成,你也解释不通 比如为何logistic ...
- 在Delphi中如何动态创建dbf数据库(二)?
unit Form_ToChangCSVforDBFU; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...
- [daily][device] linux挂载iphone
头几个月去旅游,亲戚的iphone照了好多照片,空间不足.就备份在了我的电脑上. 那么问题就是如何在linux系统里挂载iphone? 我找到了这篇文档,然而我没看. https://wiki.arc ...
- (转)js闭包初入门
先看一段JS代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 function a(){ var num = 0; function ...