C 標準庫 – string.h之memcmp使用

zhangrxiang發表於2019-05-10

memcmp

  • Compare two blocks of memory.
  • Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.

Notice that, unlike strcmp, the function does not stop comparing after finding a null character.

  • 把儲存區 lhs 和儲存區 的前 count 個位元組進行比較。
  • 比較 lhs 和 rhs 所指向物件的首 count 個位元組。比較按字典序進行。
  • 結果的符號是在被比較物件中相異的首對位元組的值(都轉譯成 unsigned char )的差。
  • 若在 lhs 和 rhs 所指向的任一物件結尾後出現訪問,則行為未定義。若 lhs 或 rhs 為空指標則行為未定義。
int memcmp( const void* lhs, const void* rhs, size_t count );

Parameters

lhs

  • Pointer to block of memory.
  • 指向記憶體塊的指標。

rhs

  • Pointer to block of memory.
  • 指向記憶體塊的指標。

count

  • Number of bytes to compare.
  • 要被比較的位元組數。

Return Value

  • Returns an integral value indicating the relationship between the content of the memory blocks:

    • < 0 the first byte that does not match in both memory blocks has a lower value in lhs than in rhs (if evaluated as unsigned char values)
    • 如果返回值 < 0,則表示 lhs 小於 rhs
    • 0 the contents of both memory blocks are equal
    • 如果返回值 > 0,則表示 lhs 小於 rhs
    • > 0 the first byte that does not match in both memory blocks has a greater value in lhs than in rhs (if evaluated as unsigned char values)
    • 如果返回值 = 0,則表示 lhs 等於 rhs

Example

//
// Created by zhangrongxiang on 2018/2/8 16:57
// File memcmp
//

#include <stdio.h>
#include <string.h>

//C 庫函式 int memcmp(const void *str1, const void *str2, size_t n)) 把儲存區 str1 和儲存區 str2 的前 n 個位元組進行比較。
//比較按字典序進行。
typedef struct {
    char *name;
} Stu;

int main() {
    char *str = "abc";
    char *str2 = "abC";
    int cmp = memcmp(str, str2, strlen(str) > strlen(str2) ? strlen(str) : strlen(str2));
    printf("%d
", cmp); // windows mingw -> 1  linux gcc 32

    int i = 0;
    int i2 = 10;
    cmp = memcmp(&i, &i2, sizeof(int));
    printf("%d
", cmp);//windows mingw -> -1  linux gcc -10
    printf("%d
", (int) sizeof(short));//2

    Stu stu = {.name = "zing"};
    Stu stu2 = {.name = "zing"};
    Stu stu3 = {.name = "Zing"};
    Stu stu4 = {.name = "aing"};

    /***********************************************/
    printf("%d
", (int) sizeof(Stu)); //8
    printf("%d
", (int) sizeof(stu.name)); //8
    printf("%d
", (int) strlen(stu.name)); //4
    printf("%d
", (int) sizeof(char *)); //8
    printf("%d
", (int) sizeof(int *)); //8
    printf("%d
", (int) sizeof(long *)); //8
    /***********************************************/

    cmp = memcmp(&stu, &stu2, sizeof(Stu));
    printf("struct %d 
", cmp);//0
    cmp = memcmp(stu.name, stu3.name, sizeof(Stu));
    printf("struct %d 
", cmp); // windows mingw -> -1  linux gcc 32
    cmp = memcmp(stu.name, stu4.name, sizeof(Stu));
    printf("struct %d 
", cmp); // windows mingw -> -1  linux gcc 25

    cmp = memcmp(&"abC", &"abc", sizeof(char *));
    printf("%d
", cmp);//-32
    printf("a -> %d;A -> %d 
", `a`, `A`);//a -> 97;A -> 65
    printf("z -> %d;a -> %d 
", `z`, `a`);//a -> 97;A -> 65
    return 0;
}

參考文章

相關文章