-
慕的地6264312
#include "stdio.h"#include "string.h"#include "malloc.h"void StringCopy(const char *strSource, char *strDestination);void main(){char *strSource={"hello world !"};char *strDestination;//给strDestination·分配内存空间,否则运行时会有异常发生strDestination = (char *) malloc (strlen(strSource) + 1);if (strDestination == NULL){printf("内存分配失败 ");return ;}StringCopy(strSource, strDestination);printf("%s ",strDestination);free(strDestination);}void StringCopy(const char *strSource, char *strDestination){int i=0; //地址计数器while(*(strSource+i)!='\0'){//不能写成 strDestination++的形式,因为strDestination是字符串的首地址*(strDestination+i)=*(strSource+i);i++;}*(strDestination+i)='\0';//给拷贝后的字符串加上结束符}
-
HUWWW
假设有2个字符串char* dst, char* src,将src中的字符复制到dst中while( *dst++ = *src++ );就可以了,其实就是strcpy函数的具体实现过程
-
白衣染霜花
#define maxsize 50#define true 1#define false 0#define other -1typedef struct{char element[maxsize];int len;}sstring;void strcopy(sstring *S,sstring T){/*½«´®TµÄÖµ¸´ÖƵ½´®SÖÐ*/int i;for(i=0;i<T.len;i++)S.element[i]=T.element[i];S->len=T.len;}