一些C库函数实现
memcpy
void* my_memcpy(void *dest, void *src, int n) { if (dest == NULL || src == NULL || n <= 0) return NULL; char* pdest = (char*) dest; char* psrc = (char*) src; if (pdest > psrc && pdest < psrc + n) { pdest = pdest + n - 1; psrc = psrc + n - 1; while (n--) { *pdest-- = *psrc--; } } else { while (n--) { *pdest++ = *psrc++; } } return dest; }
strcpy
char* my_strcpy(char* strDest, const char* strSrc) { if (strDest == NULL || strSrc == NULL) return NULL; char* p = strDest; while (*strSrc != '\0') { *strDest = *strSrc; strDest++; strSrc++; } return p; }
atoi
int my_atoi(const char* str) { if (str == NULL) return 0; int res = 0, sign = 1; while (isspace(*str)) str++; if (*str == '-') { sign = -1; str++; } else if (*str == '+') { str++; } while (*str >= '0' && *str <= '9') { res = res * 10 + (*str - '0'); str++; } res *= sign; return res; }