string - memcmp源码

//
// main.cpp
// AUTO_PRO
//
// Created by yanzhengqing on 12-12-11.
// Copyright (c) 2012年 yanzhengqing. All rights reserved.
//

#include 
#include 
using namespace std;


/*** *int memcmp(buf1, buf2, count) - compare memory for lexical order * *Purpose: * Compares count bytes of memory starting at buf1 and buf2 * and find if equal or which one is first in lexical order. * *Entry: * void *buf1, *buf2 - pointers to memory sections to compare * size_t count - length of sections to compare * *Exit: * returns < 0 if buf1 < buf2 * returns 0 if buf1 == buf2 * returns > 0 if buf1 > buf2 * *Exceptions: * *******************************************************************************/


/////////////////////////////////////////////////////////////////////////////////
/*说明: 1. __cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。被调用函数不会要求调用者传递多少参数,调用者传递过多或者过少的参数,甚至完全不同的参数都不会产生编译阶段的错误。 2. 对从两个内存地址开始的n个字符进行比较 3. 按照ANSI(American National Standards Institute)标准,不能对void指针进行算法操作,即不能对void指针进行如p++的操作,所以需要转换为具体的类型指针来操作,例如char *。(引用网友的结论) 4. size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.h的C++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。 */

int __cdecl memcmp (
                    const void * buf1,
                    const void * buf2,
                    size_t count
                    )
{
    if (!count)
        return(0);

    while ( --count && *(char *)buf1 == *(char *)buf2 ) {
        buf1 = (char *)buf1 + 1;
        buf2 = (char *)buf2 + 1;
    }

    return( *((unsigned char *)buf1) - *((unsigned char *)buf2) );//返回的是两个字符accsi的差值,当返回值为零的时候表示字符串相等
}

int main()
{
    int k = 0;
    const char src[50] = "blog.csdn.net/barry_yan";
    const char brc[50] = "blog.csdn.net/barry_yan";

    k = memcmp(src,brc,strlen(src)+1);
    if(!k)
    {
        cout<<"两个字符串相等"<
    }

    return 0;
}

你可能感兴趣的:(String,memcmp)