memcmpは、2つの指定の範囲のメモリ領域の内容が同じかどうか比較する。
Man page of MEMCMP
C言語関数辞典 - memcmp
strcmpはNULL終端までの範囲しかチェックしないが、memcmpはNULL終端関係なく指定されたバイト数分比較する。
patternとtarget、2つの文字列を比較するのだが、その前に、Xの部分に'\0'を入れて、文字列を分断している。
$ vi memcmp.c $ gcc memcmp.c -o memcmp $ ./memcmp strcmp(pattern, target) is match! memcmp(pattern, target, 4) is match! memcmp(pattern, target, 6) is match! memcmp(pattern, target, 7) is unmatch! $
strcmpは、"ABC"までしか比較しないので、一致する。
memcmpは、"ABC\0DE"まで一致するのを確認できるし、"ABC\0DEF"と"ABC\0DEG"の不一致も判別できる。
Sample/c/memcmp/memcmp/src/memcmp at master · bg1bgst333/Sample · GitHub