#include<stdio.h>#include<malloc.h>#include<assert.h>char *mystrcpy(char *strdest,const char *strsrc){ assert((strdest !=NULL )&& (strsrc !=NULL)); char *address=strdest; while((*strsrc!='\0')&&(*strdest!='\0') ) { *strdest = *strsrc; strdest++; strsrc++; } return address;}int main(void){ char *str1=(char *)malloc(5); char *str2="aaaaa"; printf(str2); mystrcpy(str1,str2); printf(str1); getchar(); return 0;}这是我自己写的mystcpy,希望能帮到你。
void main(){ char data[100] = {0}; char *a, *b; int length = 0; sanf("%c", data); b = data; while (*b) { length++; } a = (char *)malloc((length + 1) * sizeof(char)); memset(a, 0, ((length+1)*sizeof(char)); for (int index = 0; index < length; index++) { *(a + index) = *(b + index); } printf("%s\n", a);}