String operation function implementation
Memcpy Functions
The function is implemented as follows:
Void * memcpy (void * dst, const void * src, size_t n) {assert (dst! = NULL & src! = NULL & n> 0);/** in the ansic standard, void pointers are not allowed for arithmetic operations. * In the GNU standard, void pointers are allowed, the default void * is the same as char *; */char * pdst = (char *) dst; const char * psrc = (const char *) src; while (n --) {* pdst ++ = * psrc ++;} return dst ;}Memmove Function
The function is implemented as follows:
Void * memmove (void * dst, const void * src, size_t n) {assert (dst! = NULL & src! = NULL & n> 0);/** when dst and src memory do not overlap; * The replication starts from the low address; */if (dst <src) {char * pdst = (char *) dst; const char * psrc = (const char *) src; while (n --) {* pdst ++ = * psrc ++;}/** if the dst address overlaps with the src address, * the Copy starts from the high address; */else {char * pdst = (char *) dst + n-1; const char * psrc = (const char *) src + n-1; while (n --) {* pdst -- = * psrc --;} return dst ;}Strcpy Function
The function is implemented as follows:
char *strcpy(char *dst, const char *src){ if(dst == NULL || src == NULL) return NULL; char *tdst = dst; while((*tdst++ = *src++) != '/0'); return tdst;}Strncpy Function
The function is implemented as follows:
char *strncpy(char *dst, const char *src, size_t n){ if(dst == NULL || src == NULL || n <= 0) return NULL; char *tdst = dst; while(n-- && (*tdst++ = *src++) != '/0'); return tdst;}Copy function Summary
Strcpy and memcpy differ in the following three aspects:
The copied content is different: strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, structs, and classes. The copying method is different: strcpy does not need to specify the length, it ends with the string Terminator "/0. Memcpy decides the copy Length Based on its 3rd parameters. Different Uses: strcpy is usually used to copy strings, and memcpy is generally used to copy other types of data;
The difference between memmove and memcpy functions lies in the memory overlap problem. memmove functions consider the memory overlap problem, while memcpy functions do not consider overlap;
Memcmp Functions
The function is implemented as follows:
int memcmp(const void *ptr1, const void *ptr2, size_t n){ assert(ptr1 != NULL && ptr2 != NULL && n > 0); const unsigned char *tptr1 = (const unsigned char *)ptr1; const unsigned char *tptr2 = (const unsigned char *)ptr2; int res = 0; for(; n > 0; tptr1++, tptr2++, n--) { if( (res = *tptr1 - *tptr2) != 0) break; } return res;}Strcmp Functions
The function is implemented as follows:
int strcmp(const char *str1, const char *str2){ assert(str1 != NULL && str2 != NULL ); int res = 0; while(1) { if( (res = *str1 - *str2++) != 0 || !*str1++) { break; } } return res;}Strncmp Function
The function is implemented as follows:
int strncmp(const char *str1, const char *str2, size_t n){ assert(str1 != NULL && str2 != NULL && n > 0); int res = 0; while(n) { if( (res = *str1 - *str2++) != 0 || !*str1++) { break; } n--; } return res;Summary of comparison functions
Difference between strcmp and memcmp: When the strcmp function is compared, the comparison operation ends when the string Terminator '/0' is met, while the memcmp function compares the first n characters of the two strings, whether or not the string Terminator '/0' must be compared n times;
Memchr Functions
The function is implemented as follows:
void *memchr(const void *ptr, int value, size_t n){ assert(ptr != NULL && n > 0); const unsigned char *tptr = (const unsigned char *)ptr; while(n && *tptr != value) { n--; tptr++; } return (n ? (void *)tptr:NULL);}Memset Function
The function is implemented as follows:
void *memset(void *ptr, int value, size_t n){ assert(ptr != NULL && n > 0); char *tptr = (char *)ptr; while(n--) { *tptr++ = value; } return ptr;}Strlen Function
The function is implemented as follows:
size_t strlen(const char *ptr){ const char *tptr = ptr; while(*tptr++); return (tptr - ptr - 1);}Program testing
int main (){/******************* memcpy ***********************/ char buffer1[] = "DWgaOtP12df0"; char buffer2[] = "DWGAOTP12DF0"; int n; n=memcmp ( buffer1, buffer2, sizeof(buffer1) ); if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2); else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2); else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2); /*********** memchr ***********/ char * pch; char str[] = "Example string"; pch = (char*) memchr (str, 'p', strlen(str)); if (pch!=NULL) printf ("'p' found at position %d.\n", pch-str+1); else printf ("'p' not found.\n"); /************* memcpy **********/ char myname[] = "Pierre de Fermat"; /* using memcpy to copy string: */ memcpy ( person.name, myname, strlen(myname)+1 ); person.age = 46; /* using memcpy to copy structure: */ memcpy ( &person_copy, &person, sizeof(person) ); printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age ); /************* memmove **********/ char str2[] = "memmove can be very useful......"; memmove (str2+20,str2+15,11); puts (str2); /************* memset ***********/ memset (str,'-',7); puts (str); return 0;}