zoukankan      html  css  js  c++  java
  • <系统函数实现>memcmp

    这是我实现的memcmp函数:

     1 #include <stdio.h>
     2 #include <string.h>
     3 /*
     4 *int memcmp (const void *s1,const void *s2,size_t n);
     5 *º¯Êý˵Ã÷
     6 *memcmp()ÓÃÀ´±È½Ïs1ºÍs2ËùÖ¸µÄÄÚ´æÇø¼äǰn¸ö×Ö·û¡£×Ö·û´®´óСµÄ±È½ÏÊÇÒÔASCIIÂë±íÉϵÄ˳ÐòÀ´¾ö¶¨£¬´Î˳ÐòÒàΪ×Ö·ûµÄÖµ¡£memcmp()Ê×ÏȽ«s1µÚÒ»¸ö×Ö·ûÖµ¼õÈ¥s2µÚÒ»¸ö×Ö·ûµÄÖµ£¬
     7 *Èô²îΪ0ÔòÔÙ¼ÌÐø±È½Ïϸö×Ö·û£¬Èô²îÖµ²»Îª0Ôò½«²îÖµ·µ»Ø¡£ÀýÈ磬×Ö·û´®"Ac"ºÍ"ba"±È½ÏÔò»á·µ»Ø×Ö·û'A'(65)ºÍ'b'(98)µÄ²îÖµ(£­33)¡£
     8 *·µ»ØÖµ
     9 Èô²ÎÊýs1ºÍs2ËùÖ¸µÄÄÚ´æÄÚÈݶ¼ÍêÈ«ÏàͬÔò·µ»Ø0Öµ¡£s1Èô´óÓÚs2Ôò·µ»Ø´óÓÚ0µÄÖµ¡£s1ÈôСÓÚs2Ôò·µ»ØÐ¡ÓÚ0µÄÖµ¡£
    10 *p is src, q is des, len is length of p
    11 *error     =-1
    12 *p>q     >0
    13 *p<q     <0
    14 *p==q    =0
    15 */
    16 int _memcmp(const void *p,const void *q,int len)
    17 {
    18     if(!p || !q)
    19     {
    20         return -1;
    21     }
    22 
    23    // while((*(char *)p != '') && (*(char *)q != '') && (len > 0))
        while(len > 0)//this is strcmp , memcmp should't test
    24 { 25 //printf("p %p:%d q %p:%d len %d ",p,*(char *)p,q,*(char *)q,len); 26 if((*(char *)p - *(char *)q) != 0) 27 { 28 29 return (*(char *)p-*(char *)q); 30 } 31 p++; 32 q++; 33 len--; 34 } 35 36 if((len > 0 ) && (*(char *)p != '')) 37 { 38 return 1; 39 } 40 41 if((len > 0 ) && (*(char *)q != '')) 42 { 43 return -1; 44 } 45 46 return 0; 47 } 48 49 50 int main(int argc, char **argv) 51 { 52 char *p = "abcfgf"; 53 54 char *q = "abcfg"; 55 56 int val; 57 58 val = memcmp(p,q,6); 59 printf("sys val is %d ",val); 60 61 val = _memcmp(p,q,6); 62 63 printf("my val is %d ",val); 64 65 return 0; 66 }
  • 相关阅读:
    Fiddler抓包工具-拦截,断点
    python-zx笔记11-测试压力管理
    python-zx笔记10-断言
    python-zx笔记9-单元测试
    [Trouble shooting] Alt-A shortcut taken by another app and conflict with Emacs' M-a
    Dwango Programming Contest 6th Task C. Cookie Distribution
    局部有限偏序集上的 Möbius 函数
    一个求和
    一个组合问题
    ABC #150 E. Change a Little Bit
  • 原文地址:https://www.cnblogs.com/xiaowenhu/p/3210701.html
Copyright © 2011-2022 走看看