3月 142013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

strcmp源码
原型:extern int strcmp(const char *s1,const char * s2);
所在头文件:string.h
功能:比较字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)

 int __cdecl strcmp (const char * src, const char * dst)
 {
     int ret = 0 ;
     while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst&&*src)
     ++src, ++dst;
     if ( ret < 0 )
         ret = -1 ;
     else if ( ret > 0 )
         ret = 1 ;
     return( ret );
 }
神一般的 strcpy
考虑NULL指针,const修饰,地址覆盖问题
char *mystrcpy2(char* dst, const char *src)
 {
 char *ret = dst;
 int count = strlen(src)+1;
 assert(dst);
 assert(src);
 if( dst<=src || dst>=(src+count) )//地址不重合,从低字节开始复制
 {
 while(count--)
 {
 *dst++ = *src++;
 }
 }
 else //地址重合,从高字节开始复制
 {
 dst = dst + count - 1;
 src = src + count - 1;
 while(count--)
 {
 *dst-- = *src--;
 }
 }
 return dst;
 }

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)