模拟实现库函数strcpy
要实现库函数strcpy,我们应该首先要知道strcpy函数的作用。
★
strcpy(s1,s2),strcpy函数的意思是:把字符串s2拷贝到s1中,连同字符串结束标志也一同拷贝。如果s2="good",那么内存合适的s1中存放的是good\0。下面是strcpy在库函数的原型:
那么现在怎么模拟实现一个strcpy函数呢?
char *strcpy(char *dest, char *scr) { char *p = dest; assert(dest != NULL);//断言,会打印出一条错误信息 assert(scr != NULL); while (*dest++ = *scr++) { ; } return p; }不知道assert意思的请戳——> assert
下面是一个测试程序:
#include<stdio.h> #include<assert.h> int main() { char p1[] = "helloworld"; char p2[] = "good"; my_strcpy(p1, p2); printf("%s\n", p1); system("pause"); return 0; }