您的位置:首页 > 编程语言 > C语言/C++

C++字符串处理函数学习笔记

2013-08-07 17:21 573 查看
1. memcpy <cstring>

定义:

void * memcpy ( void * destination, const void * source, size_t num );

功能:

从source指定的内存块中复制num字节的数据到destination指定的内存块中。在本函数中,不关注由source和destination指定的相关对象的类型;函数返回结果为相应的二进制数据的副本。

本函数不检查source中的任何终止字符,它总是完全复制从source开始的num个字节的数据。
示例:
/* memcpy example */
#include <stdio.h>
#include <string.h>

struct {
char name[40];
int age;
} person, person_copy;

int main ()
{
char myname[] = "Pierre de Fermat";

/* using memcpy to copy string: */
memcpy ( person.name, myname, strlen(myname)+1 );
person.age = 46;

/* using memcpy to copy structure: */
memcpy ( &person_copy, &person, sizeof(person) );

printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );

return 0;
}
原文:memcpy[/code]
2.    memmove           <cstring>定义:
        void * memmove ( void * destination, const void * source, size_t num );
[code]功能:
从source指定的位置开始复制num字节的数据到destination指定的内存块。本函数允许source和destination中间有重叠的缓存。[/code]
    本函数不检查source中的任何终止字符,它总是完全复制从source开始的num个字节的数据。
示例:
/* memmove example */
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] = "memmove can be very useful......";
memmove (str+20,str+15,11);
puts (str);
return 0;
}
原文:memmove

[/code]
3.    strcpy           <cstring>定义:[code]	char * strcpy ( char * destination, const char * source );
功能:
将由source指定的C-串(包括终止符)复制到由destination指定的数组。为了避免溢出,由destination指定的应该足够长,以容纳由source指定的C-串的所有内容(包括终止符),并且destination的内存不能和source重叠。
示例:
/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}

[/code]
原文:strcpy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: