C常用库函数实现
Posted Frey_Liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C常用库函数实现相关的知识,希望对你有一定的参考价值。
void *memcpy(void* dest, const void* src, size_t n)
assert(dest != NULL && src != NULL && n > 0);
if ((src < dest) && ((char*)src + n > dest))
char* d = (char*)dest+n-1;
char* s = (char*)src+n-1;
while (n--)
*d-- = *s--;
else
char* d = (char*)dest;
char* s = (char*)src;
while (n--)
*d++ = *s++;
return dest;
// unsafe function
void *memcpy(void *dest, const void *src, size_t count)
if(dest == NULL || src == NULL || count <= 0) return NULL;
char *d = (char *)dest;
char *s = (char *)src;
while(count--)
*d = *s;
d++;
s++;
return dest;
void *memmove(void *dest, const void *src, size_t count)
if(dest == NULL || src == NULL || count <= 0) return NULL;
if(dest < src)
char *d = (char *)dest;
char *s = (char *)src;
while (count--)
*d++ = *s++;
else
char *d = (char *)dest + count;
char *s = (char *)src + count;
while (count--)
*--d = *--s;
return dest;
void* memset(void* src, int c, size_t n)
assert((src != NULL) && (n > 0));
char* psrc = (char*)src;
while (n--)
*psrc++ = (char)c;
return src;
// unsafe function
char* strcpy(char *dest, char *src)
if(dest == NULL || src == NULL)
return NULL;
char *res = dest;
while(*src != '\\0')
*dest = *src;
dest++;
src++;
*dest = '\\0';
return res;
// n <= strlen(dest)
char* strncpy(char* dest, const char* src, size_t n)
assert(dest != NULL && src != NULL && n > 0);
char *pdst = dest;
if (src < dest && src + n > dest) // strlen(src) > strlen(dest), copy first n of src to dest
pdst = dest + n - 1;
src = src + n -1;
while (n)
*pdst-- = *src--;
n--;
else
while (*src && n)
*pdst++ = *src++;
n--;
while (n--)
*pdst++ = '\\0';
return dest;
char *strcat(char *dest, char *src)
assert(dest != NULL && src != NULL);
char *head = dest;
while (*dest != '\\0')
dest++;
while (*src != '\\0')
*dest = *src;
dest++;
src++;
*dest = '\\0';
return head;
int strcmp(const char* s1,const char* s2)
assert(s1 != NULL && s2 != NULL);
while (s1 && s2 && *s1 == *s2)
s1++;
s2++;
return *s1 - *s2;
size_t strlen(const char *str)
assert(str != NULL);
int num = 0;
while(*str != '\\0')
str++;
num++;
return num;
const char* strstr(const char *str1, const char *str2)
if (str1== NULL || str2 == NULL)
return NULL;
const char *temp = str1;
const char *res = str2;
while (*str1 != '\\0')
temp = str1;
res = str2;
while (*temp== *res)
temp++;
res++;
if (*res == '\\0')
return str1;
str1++;
return NULL;
char* strchr(const char* s, int c)
if (NULL == s)
return NULL;
do
if (*s == ((char)c))
return (char *)s;
while (*s++);
return NULL;
char* strdup(char *src)
if (NULL == src)
return NULL;
char *p_src = src;
int count = 0 ;
while (*p_src != '\\0')
count++;
p_src++;
char *new_addr = (char *)malloc(sizeof(char) * (count + 1));
if (new_addr == NULL)
return NULL;
p_src = src;
while (*p_src != '\\0')
*new_addr++ = *p_src++;
*new_addr = '\\0';
return (new_addr -(count));
int isspace(int x)
if (x == ' ' || x == '\\t' || x == '\\n' || x =='\\f' || x =='\\b' || x == '\\r')
return 1;
else
return 0;
int atoi(const char* nptr)
int ret;
int sign;
while (isspace((int)(unsigned char)*nptr))
++nptr;
if ((*nptr == '-') || (*nptr == '+'))
sign = *nptr;
nptr++;
else
sign = '+';
ret = 0;
while ((*nptr <= '9') && (*nptr >= '0'))
ret = 10 * ret + (*nptr - '0');
nptr++;
if (sign == '-')
return -ret;
else
return ret;
以上是关于C常用库函数实现的主要内容,如果未能解决你的问题,请参考以下文章