memory库函数

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;

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容