メモリ比較memcmp
1940 ワード
memcmpは、メモリ領域buf 1とbuf 2を比較する前countバイトである.この関数はバイトで比較されます.
ヘッダファイル
#include
関数プロトタイプ
int memcmp(const void *buf1, const void *buf2, unsigned int count);
機能
編集
メモリ領域buf 1とbuf 2の前countバイトを比較します.
必要なヘッダファイル
編集
#includeまたは#include
戻り値
編集
buf 1
buf 1=buf 2の場合、戻り値=0
buf 1>buf 2の場合、戻り値は0より大きい
説明
編集
この関数はバイトで比較されます.
例:
s 1,s 2が文字列の場合memcmp(s 1,s 2,1)は、s 1とs 2の最初のバイトを比較するascII符号値である.
memcmp(s 1,s 2,n)は、s 1とs 2の前のnバイトを比較するascII符号値である.
例えば、char*s 1=「abc」;
char *s2="acd";
int r=memcmp(s1,s2,3);
s 1とs 2の最初の3バイトを比較し、最初のバイトは等しく、2番目のバイトの比較ではサイズが確定しており、3番目のバイトの比較を続ける必要はありません.だからr=-1.[1]
サンプルプログラム
編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
ヘッダファイル
#include
関数プロトタイプ
int memcmp(const void *buf1, const void *buf2, unsigned int count);
機能
編集
メモリ領域buf 1とbuf 2の前countバイトを比較します.
必要なヘッダファイル
編集
#includeまたは#include
戻り値
編集
buf 1
buf 1=buf 2の場合、戻り値=0
buf 1>buf 2の場合、戻り値は0より大きい
説明
編集
この関数はバイトで比較されます.
例:
s 1,s 2が文字列の場合memcmp(s 1,s 2,1)は、s 1とs 2の最初のバイトを比較するascII符号値である.
memcmp(s 1,s 2,n)は、s 1とs 2の前のnバイトを比較するascII符号値である.
例えば、char*s 1=「abc」;
char *s2="acd";
int r=memcmp(s1,s2,3);
s 1とs 2の最初の3バイトを比較し、最初のバイトは等しく、2番目のバイトの比較ではサイズが確定しており、3番目のバイトの比較を続ける必要はありません.だからr=-1.[1]
サンプルプログラム
編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include
#include
int
main()
{
char
*s1 =
"Hello,Programmers!"
;
char
*s2 =
"Hello,Programmers!"
;
int
r;
r =
memcmp
(s1,s2,
strlen
(s1));
if
(!r)
printf
(
"s1 and s2 are identical
"
);
/*s1 s2*/
elseif(r<0)
printf
(
"s1 is less than s2
"
);
/*s1 s2*/
else
printf
(
"s1 is greater than s2
"
);
/*s1 s2*/
return
0;
}
:
s1 and s2 are identical
...