C语言中字符串拷贝函数的实例

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include <string.h> /* strcpy */

void SafeCopy(char *Dest, int DestSize, char *Source);

main()

{

char Text1[20]="Tracy Sorrell"; /* string buffer */

char Text2[10]="Martin"; /* string buffer */

printf (" Original string contents are: %s\n", Text2);

SafeCopy(Text2, sizeof(Text2), Text1);

printf (" New string contents are: %s\n", Text2);

strcpy(Text2, "Alex");

printf (" Final string contents are: %s\n", Text2);

}

/****************************************************************/

void SafeCopy(

char *Dest, /* Destination buffer. */

int DestSize,

char *Source) /* Source data. */

{

/* ... Copy 'Source' into 'Dest'.

* ... 'Dest' is padded with NULLs if 'Source' is smaller.. */

strncpy(Dest, Source, DestSize);

/* ... Safety net! Add the NULL just in case 'Source' is larger

* ... than 'Dest'. */

Dest[DestSize-1] = '\0';

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航