The simulation implements the memory comparison function memcmp:
The function is similar to strcmp, and can be used for the same string comparison, and returns a value of 0 if the same. If the former is greater than the latter, an integer value greater than 0 is returned, or an integer value less than 0 is returned.
The difference is:
STRCMP can only compare strings, memcmp is a memory comparison function, in principle it is compared to memory, but in fact not all can be compared when actually implemented, for example, float, but we can at least compare strings and int type. For strcmp,strncmp comparison: str1, str2 is the two string to compare, n is the number of characters to compare, and the function strcmp () does not, strcmp () can compare all the strings (because it is looking for the end of the string "\").
For the implementation of strcmp, you can view my blog http://10740184.blog.51cto.com/10730184/1714512
For the implementation of STRNCMP, you can view my blog http://10740184.blog.51cto.com/10730184/1715207
The code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h> #include <stdlib.h> #include <assert.h >INT&NBSP;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&NBSP;&&&NBSP;*DEST&NBSP;==&NBSP;*SRC) { count--; dest++; src++; } if ( count == 0) return 0; else return *dest - *src ;} Int main () { /*int arr1[] = { 1, 3, 20, 5 }; //Comparison integral type int arr2[] = { 1, 3 , 10, 7 };*/ char arr1[] = "Hello"; //Comparing strings 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;}
"Face Test" C language: Simulation to achieve memcmp, try to compare the difference between memcmp and strcmp,strncmp