`
eriol
  • 浏览: 401085 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

strcpy, memcpy, memmove, memset的实现

    博客分类:
  • C++
阅读更多

strcpy() 字符串拷贝

 

char *strcpy(char *strDest, const char *strSrc)
{
    assert((strDest!=NULL) && (strSrc !=NULL));
    char *address = strDest;     
    while( (*strDest++ = * strSrc++) != '\0'); 
    return address ;       
}

 

 

memcpy 拷贝不重叠的内存块 

 

void *memcpy(void* pvTo, void* pvFrom, size_t size) //byte是java里的变量类型
{
    assert(pvTo != NULL && pvFrom != NULL);
    byte* pbTo = (byte*)pvTo;
    byte* pbFrom = (byte*)pvFrom;
    /* 内存块重叠吗?如果重叠,就使用memmove */
    assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
    while(size-->0)
        *pbTo++ == *pbFrom++;
    return pvTo;
}

 

 

memmove 可拷贝重叠的内存块

 

void* memmove(void *dest, const void *src,size_t n) 
{ 
    if (n == 0) return 0; 
    if (dest == NULL) return 0; 
    if (src == NULL)    return 0; 
    char *psrc = (char*)src; 
    char *pdest = (char*)dest; 
    if((dest <= psrc) || (pdest >= psrc + n)) /*检查是否有重叠问题 */ 
    { 
         for(int i=0; i < n; i++) /*正向拷贝*/ 
         { 
              *pdest = *psrc; 
               psrc++; 
               pdest++; 
         } 
    } 
    else /*反向拷贝*/ 
    { 
        psrc += n; 
        pdest += n; 
        for(int i=0;i<n;i++) 
        { 
            psrc--; 
            pdest--; 
            *pdest = *psrc; 
        } 
    } 
    return dest;
}
 

memset 把buffer所指内存区域的前count个字节设置成字符cvoid * memset(void* buffer, int c, int count)
{
    char* pvTo=(char*)buffer;
    assert(buffer != NULL);
    while(count-->0)
    *pvTo++=(char)c;
    return buffer;
}

分享到:
评论

相关推荐

    C语言函数库函数详细介绍手册

    memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...

    C 语言库函数使用手册

    memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...

    C语言库函数速查 CHM

    memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...

    C语言函数速查手册

    memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...

    C语言函数速查

    memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...

    常见面试需要实现的函数std_func.c

    * 常见的字符串函数实现: * strlen * strcpy strncpy * strcmp strncmp * strcat strncat * strstr * * 内存操作: * memset、memcmp、memcpy、memmove * * 字符串和数组转换: * atoi itoa *

    C语言字符串各函数-具体实现

    strcpy,strncpy,strcat,strncat,strcmp,strncmp,strchr,strnchr,strlen,strnlen,strspn,strpbrk,strtok,strsep,memset,bcopy,memcpy,memmove,memcmp,memscan,strstr,memchr.函数具体实现内容。对理解C语言和C编程有...

    C标准库函数.CHM

    2.14.5 memmove 2.14.6 memset 2.14.7 strcat 2.14.8 strncat 2.14.9 strchr 2.14.10 strcmp 2.14.11 strncmp 2.14.12 strcoll 2.14.13 strcpy 2.14.14 strncpy 2.14.15 strcspn 2.14.16 strerror 2.14...

    C语言讲义.doc

    1.1.22 memset,memcpy,memmove函数 61 1.1.23 指针小结 63 2 字符指针与字符串 64 2.1 指针和字符串 64 2.2 通过指针访问字符串数组 64 2.3 函数的参数为CHAR * 64 2.4 指针数组做为MAIN函数的形参 65 3 内存管理 65...

    c/c++函数库说明(api)html版

    memmove (stdstring) memset (stdstring) merge (cpplist) mktime (stddate) modf (stdmath) none (cppbitset) open (cppio) peek (cppio) perror (stdio) pop (cpppriorityqueue) pop (cppqueue) pop ...

    -C++参考大全(第四版) (2010 年度畅销榜

    26.15 memmove函数 26.16 memset函数 26.17 strcat函数 26.18 strchr函数 26.19 strcmp函数 26.20 strcoll函数 26.21 strcpy函数 26.22 strcspn函数 26.23 strerror函数 26.24 strlen函数 26.25 strncat函数 26.26 ...

    linux_c API函数大全

    memset(将一段内存空间填入某值) 40 5.11 40 rindex(查找字符串中最后一个出现的指定字符) 40 5.12 41 strcasecmp(忽略大小写比较字符串) 41 5.13 41 strcat(连接两字符串) 41 5.14 42 strchr(查找字符串中...

Global site tag (gtag.js) - Google Analytics