#include<stdio.h>标签:++,char,strcpy,arr2,arr1,拷贝,NULL,my From: https://blog.51cto.com/u_13606048/6111332
#include<assert.h>//为了引用assert
void my_strcpy1(char* arr1, char* arr2) {
while (*arr2 != '\0') {
*arr1 = *arr2;
arr1++;
arr2++;
}
*arr1 = *arr2;
}
void my_strcpy2(char* arr1, char* arr2) {
while (*arr1++=*arr2++) {
; //简洁
}
}
void my_strcpy3(char* arr1, char* arr2) {
assert(arr1 != NULL);
assert(arr2 != NULL); //用来防止其中一个是空指针
while (*arr1++ = *arr2++) {
;
}
}
void my_strcpy4(char* arr1,const char* arr2) {
assert(arr1 != NULL);
assert(arr2 != NULL);
while (*arr1++ = *arr2++) { //防止此处写反
;
}
}
char* my_strcpy5(char* arr1, const char* arr2) {
char* str = arr1;
assert(arr1 != NULL);
assert(arr2 != NULL);
while (*arr1++ = *arr2++) {
;
}
return str; //链式访问
}
int main() {
char arr1[] = "#######";
char arr2[] = "abcd";
printf("%s\n", arr1);
//my_strcpy1(arr1, arr2);
//my_strcpy2(arr1, arr2);
//my_strcpy3(arr1, arr2);
//my_strcpy4(arr1, arr2);
printf("%s\n", my_strcpy5(arr1, arr2));
//printf("%s\n", arr1);
return 0;
}