模拟实现strncpy
#include <stdio.h> #include <windows.h> #include <string.h> #include <assert.h> char * My_strncpy(char * dest, const char *src, size_t n) { assert(dest); assert(src); char *ret = dest; while (n--) { *dest++ = *src++; } return ret; } int main() { char str1[20] = "123456789"; char str2[20] = "abcde"; printf("调用My_strncpy之前: str1: %s \n", str1_1); My_strncpy(str1, str2, 5); //调用My_strncpy printf("调用My_strncpy之后: str1: %s \n", str1); system("pause"); return 0; } 文章来源: 模拟实现strncpy