Memory function

// memory function
#include <stdio.h>
#include <memory.h>
#include <strings.h>
void main2 () {// memset mutator
    char str[45] = "hello luoxu hello c";
    memset (str, 'A', 6); // first parameter memory first address, the second parameter value to be assigned, the third parameter number of bytes from the head address proceeds
    printf("%s\n",str); //AAAAA luoxu hello c

    memset (str, '0', strlen (str)); // set all 0
    printf("%s,%d\n",str,strlen(str));//0000000000000000000,19

    memset (str, '\ 0', strlen (str)); // empty string
    printf("%s,%d\n",str,strlen(str));//,0
}

void main3 () {// memcpy copy string
    char str1[30] ="China is great";
    char str2[30] ="hello c";
    printf("%s\n",str1); //China is great
    memcpy (str1, str2,5); // Take five characters replace the contents of the first address 5 str1 starting from the number of bytes in str2
    printf("%s\n",str1); //hello is great
}
void main4 () {// memcpy copies array
    int a[]={1,3,5,7,9};
    int b[]={2,4,6,8,10};
    memcpy (a, b, 8); // b starts is removed from the first address of the 8-byte binary number, the binary number assigned to a first address from the first 8 bytes, i.e., a replacement of the first two element
    // memcpy accordance memory bytes to copy, no matter what type, is a copy of the binary, so the four-byte integer can also copy in bytes
    for (int i = 0; i < 5; ++i) {
        printf("%d,",a[i]);  //2,4,5,7,9,
    }
}

void main5 () {// memccpy copy string, a character ends
    char str1[30] ="China is great";
    char str2[30] ="hello c";
    printf("%s\n",str1); //China is great
    // memccpy (str1, str2, '0', 7); // taken from five characters in str2 (met '0' to the end of the replacement) replace the contents from the first address of str1 7 represents the number of bytes, this when equivalent memcpy ()
    memccpy (str1, str2, 'l', 7); // str2 taken from the first address from the start to 'l' of the string, which is the replacement string from the first address of str1
    // effects: sometimes can use this feature to set a terminator character
    printf("%s\n",str1); //helna is great
}

void main6 () {// memchr look up the address of a character in a string
    char str[30] ="China is great";
    char ch='i';
    char *p = memchr(str,ch,30);
    // start retrieving the address str, forward 30 bytes, returns its address is present, there is no return to NULL
    if(p ==NULL) printf("not found");
    else printf("found");
}

void main () {// memicmp case-insensitive comparison string is equal to ignore
    char *buf1 ="ABCD123";
    char *buf2 ="abcd456";
    int res = memicmp(buf1,buf2,4);
    // compare buf1 and buf2 the first four bytes are equal, ignoring case, to return to their equal 0, ranging from non-zero return
    if (res == 0) printf ( "The first four characters equal ignoring case");
    else printf ( "first four characters of equal ignoring case");
}

 

Guess you like

Origin www.cnblogs.com/luoxuw/p/11331137.html