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

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

2    memmove           <cstring>

定义:

        void * memmove ( void * destination, const void * source, size_t num );
功能:
source指定的位置开始复制num字节的数据到destination指定的内存块。本函数允许sourcedestination中间有重叠的缓存。
        本函数不检查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
    
    

3    strcpy           <cstring>

定义:

	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;
}

原文:strcpy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值