The memcmp() function compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long.
변수명 | 설명 |
---|---|
s1 | 비교할 메모리 시작위치1 |
s2 | 비교할 메모리 시작위치2 |
n | 비교할 데이터 크기 (byte 단위) |
음수 : s1과 s2에서 다른데이터가 나왔을 때의 차이 (*s1 - *s2)
다를 때 s2의 char값이 더 큼
0 : n바이트 모두 같은 값
양수 : s1과 s2에서 다른데이터가 나왔을 때의 차이 (*s1 - *s2)
다를 때 s1의 char값이 더 큼
int memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *s1_ptr;
unsigned char *s2_ptr;
size_t i;
i = 0;
s1_ptr = (unsigned char *)s1;
s2_ptr = (unsigned char *)s2;
while (i < n)
{
if (s1_ptr[i] != s2_ptr[i])
return (s1_ptr[i] - s2_ptr[i]);
i++;
}
return (0);
}