使用两种不同的方式复制字符串

    技术2022-05-12  30

     

     使用两种不同的方法复制字符串到数组当中,cpfile1使用的是数组元素逐个复制,cpfile2使用的是指针加偏移量

     逐个复制

     

     #include <STDIO.H> void cpfile1( char *s1, const char *s2 ); void cpfile2( char *s1, const char *s2 ); int main() { char str1[10]; char str2[]="hello world"; char str3[10]; char *str4="good bye"; cpfile1( str1, str2 ); cpfile2( str3, str4 ); printf( "copy string1 is %s/n", str1 ); printf( "copy string3 is %s/n", str3 ); return 0; } void cpfile1( char *s1, const char *s2 ) { int i; for( i=0;(s1[i]=s2[i])!='/0';i++ ); } void cpfile2( char *s1, const char *s2) { for( ;(*s1=*s2)!='/0';s1++, s2++ ); }


    最新回复(0)