一.memcpy(void* dest,void* src,int num) ,操作单位为字节,完成复制且粘贴字符串
1.应用
#include <stdio.h>
#include<string.h>
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };memcpy(arr2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2int i = 0;for (i = 0; i < 5; i++){printf("%d ", arr2[i]);}return 0;
}
2.模拟实现
#include <stdio.h>
#include<string.h>
#include<assert.h>
//因为该函数需要能操控任意类型的数据,故不能将参数设定为固定类型
//而void类型可以接收任意类型的值
void* my_memcpy(void* arr2,const void* arr1,int x)//将arr1中的元素拷贝给arr2
{assert(arr1&&arr2); //不能为空指针void* ret = arr2; //保存被赋值数组的首地址while (x--){*(char*)arr2 = *(char*)arr1; //void*类型无法解引用,需要转换为其他类型arr1 = (char*)arr1 + 1; //转化为char*类型,可每次只改变一个字节,可适用于任何类型arr2 = (char*)arr2 + 1;}return ret; //返回被赋值数组的首地址
}
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };my_memcpy(arr2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2int i = 0;for (i = 0; i < 5; i++){printf("%d ", arr2[i]);}return 0;
}
3.重点——memcpy()可以处理,但不用来处理重叠内存之间的数据拷贝(这种问题统一交给memmove函数——分工明确)
#include <stdio.h>
#include<string.h>
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };memcpy(arr1+2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2int i = 0;for (i = 0; i < 5; i++){printf("%d ", arr2[i]);}return 0;
}
二.memmove(void* dest,void* src,int num) ——解决重叠内存之间的数据拷贝
1.应用
#include <stdio.h>
#include<string.h>
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };memmove(arr1 + 2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2int i = 0;for (i = 0; i < 5; i++){printf("%d ", arr1[i]);}return 0;
}
2.模拟实现
#include <stdio.h>
#include<string.h>
#include<assert.h>
void* my_memmove(void* arr2 ,void* arr1, int x)//将arr1中的元素拷贝给arr2
{void* p = arr2;assert(arr2&&arr1);if (arr2 <= arr1){while (x--){*(char*)arr2 =*(char*)arr1;arr1=(char*)arr1+1;arr2=(char*)arr2+1;}}else{while (x--){*((char*)arr2 + x) = *((char*)arr1+x); //将指针指向需要赋值的空间最后-1,每次向前读取一个字节}}return p; //返回被拷贝的数组首地址
}
int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };my_memmove(arr1 + 2, arr1, 28);//从arr1中读取20个字节,将其复制给arr1+2int i = 0;for (i = 0; i < 10; i++){printf("%d ", arr1[i]);}return 0;
}
三.memcmp()——内存比较函数,比较单位为字节,可以比较任意类型的元素
1.应用
#include<stdio.h>
#include<string.h>
int main()
{int arr1[] = { 1,2,3,4,5}; //01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00int arr2[20] = { 1,3,2 }; //01 00 00 00 03 00 00 00 02 00 00 00int ret = memcmp(arr1,arr2,12);//比较两个数组中前12个字节//arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1<arr2返回值为负整数printf("%d",ret);return 0;
}
2.模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_memcmp(void* arr1, void* arr2, int x)
{assert(arr1 && arr2);while (*(char*)arr1 == *(char*)arr2) //相同的情况下,判断下一位是否也相同,不相同则退出循环{if (x==0) //若有其中一个字符串到达末尾,则退出循环{break;}arr1=(char*)arr1+1;arr2=(char*)arr2+1;x--;}return *(char*)arr1 - *(char*)arr2; //不相同则相减返回差值
}
int main()
{int arr1[] = { 1,2,3,4,5}; //01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00int arr2[20] = { 1,3,3 }; //01 00 00 00 03 00 00 00 02 00 00 00int ret = my_memcmp(arr1,arr2,12);//比较两个数组中前12个字节//arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1<arr2返回值为负整数printf("%d",ret);return 0;
}
四.memset()——内存初始化(初始化变量为整形或字符型)
1.应用
#include<stdio.h>
#include<string.h>
int main()
{char arr[] = "hello bit";memset(arr,'x',5); //将arr地址后五个字节的内存初始化为‘x’printf("%s",arr);return 0;
}
2.模拟实现
#include<stdio.h>
#include<string.h>
void* my_memset(void* arr, int value, int x)
{void* p = arr;while (x--){*(char*)arr = value;arr=(char*)arr + 1;}return p;
}
int main()
{char arr[] = "hello bit";my_memset(arr+6,'1',3);printf("%s",arr);return 0;
}