公司网站推广方法,做互联网的网站,wordpress仪表盘登录,html简单网页设计作品大家好#xff0c;我是残念#xff0c;希望在你看完之后#xff0c;能对你有所帮助#xff0c;有什么不足请指正#xff01;共同学习交流 本文由#xff1a;残念ing 原创CSDN首发#xff0c;如需要转载请通知 个人主页#xff1a;残念ing-CSDN博客#xff0c;欢迎各位…大家好我是残念希望在你看完之后能对你有所帮助有什么不足请指正共同学习交流 本文由残念ing 原创CSDN首发如需要转载请通知 个人主页残念ing-CSDN博客欢迎各位→点赞 收藏⭐️ 留言 系列专栏残念ing 的C语言系列专栏——CSDN博客 目录
前言
1. memcpy 函数
1.1 memcpy 的使用
1.2 memcpy 的模拟实现
2. memmove 函数
2.1 memmove 的使用
2.2 memmove 的模拟实现
3. memset 函数的使用
4. memcmp 函数的使用 前言
在C语言中除了字符函数和字符串函数外还有关于内存的函数现在我们就来学习一下内存函数吧
1. memcpy 函数
void * memcpy ( void * destination, const void * source, size_t num );功能函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置
注意
1、 这个函数在遇到\0的时候并不会停下来
2、如果source和destination有任何的重叠复制的结果都是未定义的
1.1 memcpy 的使用
#includestdio.h
#includestring.h
//memcpy的使用--拷贝有开始地址拷贝的数
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,0 };int arr2[20] { 0 };memcpy(arr2, arr13, 5 * sizeof(int));for (int i 0; i 20; i){printf(%d , arr2[i]);}return 0;
}
1.2 memcpy 的模拟实现
//模拟实现
void* my_memcpy(void* dest, const void* src, size_t num)
{while (num--){*(char*)dest *(char*)src;dest (char*)dest 1;src (char*)src 1;}return dest;
}
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,0 };int arr2[20] { 0 };void*retmy_memcpy(arr2, arr1 3, 5 * sizeof(int));for (int i 0; i 20; i){printf(%d , arr2[i]);}return 0;
}
2. memmove 函数
void * memmove ( void * destination, const void * source, size_t num );
功能从source的位置开始向后复制num个字节的数据到destination指向的内存位置
注意
1、和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的
2、如果源内存空间和目标空间出现重叠就得使用memmove函数处理
2.1 memmove 的使用
int main()
{int arr1[] { 1,2,3,4,5,6,7,8,9,0 };int arr2[20] { 0 };memmove(arr12, arr1, 5 * sizeof(int));for (int i 0; i 10; i){printf(%d , arr1[i]);}return 0;
}
2.2 memmove 的模拟实现
void* memmove(void* dst, const void* src, size_t count)
{void* ret dst;//记住起始位置if (dst src || (char*)dst ((char*)src count)) {//从前往后拷while (count--) {*(char*)dst *(char*)src;dst (char*)dst 1;src (char*)src 1;}}else {//从后往前拷while (count--) {*((char*)dstcount) *((char*)srccount);}}return ret;
}
3. memset 函数的使用
void * memset ( void * ptr, int value, size_t num );
功能memset是用来设置内存的将内存中的值以字节为单位设置成想要的内容
#include stdio.h
#include string.h
int main()
{char str[] hello world;memset(str, x, 6);printf(str);return 0;
}
4. memcmp 函数的使用
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
功能比较从ptr1和ptr2指针指向的位置开始向后的num个字节
返回规则 #include stdio.h
#include string.h
int main()
{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);elseprintf(%s is the same as %s.\n, buffer1, buffer2);return 0;
}