strcpy实现
#include <iostream> #include <stdio.h> using namespace std; char *_strcpy(char* des, char* src) { if (des == NULL || src == NULL) { return 0; } // cout << (void*)des << endl; // cout << src << endl; // cout << *des << endl; // cout << *src << endl; int ret = 0; char* temp1 = des; char* temp2 = src; while (!(ret=*temp1 -*temp2) && *temp1) { temp1++; temp2++; } if (ret == 0) { return 0; } char* pIter = des; while ((*pIter++ = *src++) != '\0'); return des; } int main() { char a[] = "abc"; char b[] = "abc4"; char *c = _strcpy(a, b); return 0; }
如果有对 while ((*pIter++ = *src++) != '\0'); 不理解的,可以写成这样
while (*pIter != '\0') { *++pIter = *++src; }