`
anders0913
  • 浏览: 89658 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

memcpy, strncpy, strcpy

阅读更多
收集的一些基本函数实现手法,备忘~~

memcpy
/**
* memcpy - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
void * memcpy(void * dest,const void *src,size_t count)
{
    char *tmp = (char *) dest, *s = (char *) src;

    while (count--)
    *tmp++ = *s++;

    return dest;
}


strncpy
/**
* strncpy - Copy a length-limited, %NUL-terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
* However, the result is not %NUL-terminated if the source exceeds
* @count bytes.
*/
char * strncpy(char * dest,const char *src,size_t count)
{
    char *tmp = dest;

    while (count-- && (*dest++ = *src++) != '\0')
        /* nothing */;

    return tmp;
}


strcpy
/**
* strcpy - Copy a %NUL terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
*/
char * strcpy(char * dest,const char *src)
{
    char *tmp = dest;

    while ((*dest++ = *src++) != '\0')
        /* nothing */;
    return tmp;
}


FreeBSD中strcpy
char *strcpy(char * __restrict to, const char * __restrict from)
{
    char *save = to;

    for (; (*to = *from); ++from, ++to);
    return(save);
}


OpenBSD 3.9中strcpy
char *strcpy(char *to, const char *from)
{
    char *save = to;

    for (; (*to = *from) != ''; ++from, ++to);
    return(save);
}




:-)



分享到:
评论

相关推荐

    安全函数strcpy_s、strncpy_s、snprintf_s、memcpy_s

    用于了解安全函数strcpy_s、strncpy_s、snprintf_s、memcpy_s

    strncat strncpy strncmp memcpy memcmp 比较及其原型

    strcmp strcpy strcat strlen 的实现 以及与strncat strncpy strncmp memcpy memcmp 原型分析

    25_strcpy_strncpy_memcpy的区别1

    1. 复制的内容不同 2. 复制的法不同 3. 途不同 1. dest指向的空间要够拷 3. 使strncpy时,确保dest的最后个字符是“\0”

    C语言 strcpy和memcpy区别详细介绍

    C语言 strcpy和memcpy区别详细介绍 PS:初学算法,开始刷leetcode,Rotate array的预备知识(写的代码Time Limit Exceed难过)于是百度高效算法,本篇作为预备知识。 1、strcpy和strncpy函数 这个不陌生,大一学...

    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/C++面试之算法系列--几个典型的内存拷贝及字符串函数实现

    void* memcpy( void *dest, const void *src, size_t count ) { char* pdest = static_cast*>( dest ); const char* psrc = static_cast*>( src ); // 依次从前拷贝,目的地址覆盖了源地址的数,此时从后往前...

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

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

    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语言字符串各函数-具体实现

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

    四个拷贝函数的分析与实现

     1:strncpy和strncpy主要是用于字符串的拷贝。  2:而memcpy()和memmove()则适用于所有的数据类型。  3: memcpy()和memmove()这两者的区别在于内存重叠的处理。  4:本文给出的代码都是基于函数的功能所写的...

    C/C++字符串函数之复制函数详解

    1、char * strcpy(char* destination,const char * source); 2、char* strncpy(char* destination,const char* source,size_t num); 3、void * memcpy(void* destination,const void* source,size_t num); 4、void *...

    C标准库函数.CHM

    The C Library Reference Guide by Eric Huss ...Introduction 1. Language 1.1 Characters 1.1.1 Trigraph Characters ... 2.14.14 strncpy 2.14.15 strcspn 2.14.16 strerror 2.14.17 strlen 2.14.18 strpbrk ...

    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版

    strncpy (stdstring) strpbrk (stdstring) strrchr (stdstring) strspn (stdstring) strstr (stdstring) strtod (stdstring) strtok (stdstring) strtol (stdstring) strtoul (stdstring) strxfrm (std...

    c++ 面试题 总结

    建议使用 strncpy 和 memcpy -------------------------------------------------------------------------- 12.下面代码有什么问题? #define MAX_SRM 256 DSN get_SRM_no() { static int SRM_no; //是不是...

    linux_c API函数大全

    memcpy(拷贝内存内容) 39 5.9 40 memmove(拷贝内存内容) 40 5.10 40 memset(将一段内存空间填入某值) 40 5.11 40 rindex(查找字符串中最后一个出现的指定字符) 40 5.12 41 strcasecmp(忽略大小写比较字符串...

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

    26.27 strncpy函数 26.28 strpbrk函数 26.29 strrchr函数 26.30 strspn函数 26.31 strstr函数 26.32 strtok函数 26.33 strxfrm函数 26.34 tolower函数 26.35 toupper函数 第27章 数学函数 27.1 acos函数 27.2 asin...

Global site tag (gtag.js) - Google Analytics