strncpy,strcpy,memcpy的区别
来源:互联网 发布:火星潮牌淘宝 编辑:程序博客网 时间:2024/04/27 21:36
strcpy和memcpy都是标准C库函数,它们有下面的特点:
strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。
已知strcpy函数的原型是:char* strcpy(char* dest, const char* src);
memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。
void *memcpy( void *dest, const void *src, size_t count );
char
*
strcpy
(
char
* dest,
const
char
* src)
// 实现src到dest的复制
{
if
((src == NULL) || (dest == NULL))
//判断参数src和dest的有效性
{
return
NULL;
}
char
*strdest = dest;
//保存目标字符串的首地址
while
((*strDest++ = *strSrc++)!=
'\0'
);
//把src字符串的内容复制到dest下
return
strdest;
}
void
*
memcpy
(
void
*memTo,
const
void
*memFrom,
size_t
size)
{
if
((memTo == NULL) || (memFrom == NULL))
//memTo和memFrom必须有效
return
NULL;
char
*tempFrom = (
char
*)memFrom;
//保存memFrom首地址
char
*tempTo = (
char
*)memTo;
//保存memTo首地址
while
(size -- > 0)
//循环size次,复制memFrom的值到memTo中
*tempTo++ = *tempFrom++ ;
return
memTo;
}
strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy
memcpy
Summary#include <string.h>void *memcpy ( void *dest, /* destination buffer */ void *src, /* source buffer */ int len); /* bytes to copy */
The memcpy function copies len bytes from src to dest. If these memory buffers overlap, the memcpy function cannot guarantee that bytes insrc are copied to dest before being overwritten. If these buffers do overlap, use the memmove function.
Return ValueThe memcpy function returns dest.
See Alsomemccpy, memchr, memcmp,memmove, memset
Example#include <string.h>#include <stdio.h> /* for printf */void tst_memcpy (void) { char src1 [100] = "Copy this string to dst1"; char dst1 [100]; char *p; p = memcpy (dst1, src1, sizeof (dst1)); printf ("dst = \"%s\"\n", p);}
strcpy
Summary#include <string.h>char *strcpy ( char *dst, /* destination string */ char *src); /* source string */
The strcpy function copies characters from src to dst up to and including the terminating null character.
Return ValueThe strcpy function returns dst.
See Alsostrcat, strlen, strncat,strncpy
Example#include <string.h>#include <stdio.h> /* for printf */void tst_strcpy (void) { char buf [21]; char s [] = "Test String"; strcpy (buf, s); strcat (buf, " #2"); printf ("new string is %s\n", buf);}
strncpy
Summary#include <string.h>char *strncpy ( char *dst, /* destination string */ char *src, /* source string */ int len); /* max characters to copy */
The strncpy function copies at most len characters from src todst. Characters are copied until a null character ('\0') is copied or untillen characters have been copied. If the length ofsrc is less than len the remaining bytes in dst are padded with null characters ('\0').
Return ValueThe strncpy function returns dst.
See Alsostrcat, strcpy, strlen,strncat
Example#include <string.h>#include <stdio.h> /* for printf */void tst_strncpy (char *s) { char buf [21]; strncpy (buf, s, sizeof (buf)); buf [sizeof (buf) -1] = '\0';}
- strncpy,strcpy,memcpy的区别
- strcpy、strncpy、memcpy的区别
- strcpy、strncpy、memcpy的区别
- strcpy、strncpy和memcpy的区别
- memcpy && strncpy的区别,memcpy && strcpy的区别
- memcpy,strcpy,strncpy的用法
- bcopy(), memccpy(), memcpy(), strcpy(), strncpy()的区别与使用
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy strncpy memcpy 的用法与区别1
- strcpy strncpy memcpy 的用法与区别 2
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy、strncpy与memcpy的区别与使用方法
- strcpy、strncpy与memcpy的区别和使用方法
- strcpy、strncpy与memcpy的区别与使用方法
- C之memset、memcpy、strcpy、strncpy区别
- 使View变圆的方法
- xcode编译错误总结
- html布局+浮动效果
- UVA 103
- JDK常用命令
- strncpy,strcpy,memcpy的区别
- EPop expired后怎么办
- SVN服务器的搭建和使用
- 在rails中使用FusionCharts生成报表
- 又是一个苦逼 的 change
- 强制类型转换
- java 实现的用递归法实现全排列问题
- CSS让图片垂直居中的几种技巧
- STL源码剖析(一) - 内存分配