美文网首页
memory库函数

memory库函数

作者: 康凯_7f06 | 来源:发表于2019-11-23 14:29 被阅读0次

    1.memset

    作用:将数组中的指定长度的字节设置为指定的字符

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    int main()

    {

    char str[20] = "hello kk, hello c";

    memset(str, 'A', 5);  // 将数组中的指定长度的字节设置为指定的字符

    printf("%s", str);

    system("pause");

    return 0;

    }

    2.memcpy

    作用:从源source中拷贝n个字节到目标destin中

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    int main()

    {

    char dest[20] = "hello kk, hello c";

    char sour[20] = "newstr";

    printf("原来的字符串为:%s\n", dest);

    memcpy(dest, sour, 3);  // 从源source中拷贝n个字节到目标destin中

    printf("复制后字符串为:%s", dest);

    system("pause");

    return 0;

    }

    3._memccpy

    作用:从源source中拷贝最多n个字节到目标destin中,遇到源中特定字符停止

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    int main()

    {

    char dest[20] = "hello kk, hello c";

    char sour[20] = "newstr";

    printf("原来的字符串为:%s\n", dest);

    _memccpy(dest, sour, 'w' , 6);  // 从源source中拷贝最多n个字节到目标destin中,遇到源中特定字符停止

    printf("复制后字符串为:%s", dest);

    system("pause");

    return 0;

    }

    4.memchr

    作用:在数组的前n个字节中搜索字符,返回搜索到的字符地址

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    #include<string.h>

    int main()

    {

    char str[20] = "hello kk, hello c";

    char *p;

    p = memchr(str, 'k', strlen(str));  // 在数组的前n个字节中搜索字符,返回搜索到的字符地址

    if (p)

    {

    printf("找到字符%c", *p);

    }

    else

    {

    printf("不存在该字符");

    }

    system("pause");

    return 0;

    }

    5. _memicmp

    作用:比较两个串s1和s2的前n个字节, 忽略大小写

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    int main()

    {

    char str1[20] = "hello kk, hello c";

    char str2[20] = "HELLO jj, hello c++";

    int res = _memicmp(str1, str2, 7);  // 比较两个串s1和s2的前n个字节, 忽略大小写

    if (res == 0)

    {

    printf("匹配的字符串忽略大小写相同\n");

    }

    else

    {

    printf("匹配的字符串不相同\n");

    }

    system("pause");

    return 0;

    }

    6. memmove 

    作用: 移动一块字节,与memcpy类似

    #include<stdio.h>

    #include<stdlib.h>

    #include<memory.h>

    int main()

    {

    char dest[20] = "hello kk, hello c";

    char sour[20] = "*************";

    printf("原来的字符串为:%s\n", dest);

    memmove(dest, sour, 3);  // 移动一块字节,与memcpy类似

    printf("复制后字符串为:%s", dest);

    system("pause");

    return 0;

    }

    相关文章

      网友评论

          本文标题:memory库函数

          本文链接:https://www.haomeiwen.com/subject/oimfwctx.html