#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<assert.h> int my_memcmp(const void *p1, const void *p2, size_t count) { assert(p1); assert(p2); char *dest = (char *)p1; char *src = (char *)p2; while (count && *dest == *src) { count--; dest++; src++; } if (count == 0) return 0; else return *dest - *src ; } int main() { /*int arr1[] = { 1, 3, 20, 5 }; //比較整型 int arr2[] = { 1, 3, 10, 7 };*/ char arr1[] = "hello"; //比較字符串 char arr2[] = "hello world"; int len = sizeof(arr1) / sizeof(arr1[0]); int ret = my_memcmp(arr1, arr2, 12); printf("%d", ret); system("pause"); return 0; }