- #include
- #include
- int main()
- {
- int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
- int arr2[20] = { 0 };
- memcpy(arr2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- printf("%d ", arr2[i]);
- }
- return 0;
- }
- #include
- #include
- #include
- //因为该函数需要能操控任意类型的数据,故不能将参数设定为固定类型
- //而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个字节,将其复制给arr2
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- printf("%d ", arr2[i]);
- }
- return 0;
- }
- #include
- #include
- 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个字节,将其复制给arr2
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- printf("%d ", arr2[i]);
- }
- return 0;
- }
- #include
- #include
- 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个字节,将其复制给arr2
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- printf("%d ", arr1[i]);
- }
- return 0;
- }
- #include
- #include
- #include
- 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+2
- int i = 0;
- for (i = 0; i < 10; i++)
- {
- printf("%d ", arr1[i]);
- }
- return 0;
- }
- #include
- #include
- 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 00
- int arr2[20] = { 1,3,2 }; //01 00 00 00 03 00 00 00 02 00 00 00
- int ret = memcmp(arr1,arr2,12);//比较两个数组中前12个字节
- //arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1
- printf("%d",ret);
- return 0;
- }
- #include
- #include
- #include
- 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 00
- int arr2[20] = { 1,3,3 }; //01 00 00 00 03 00 00 00 02 00 00 00
- int ret = my_memcmp(arr1,arr2,12);//比较两个数组中前12个字节
- //arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1
- printf("%d",ret);
- return 0;
- }
- #include
- #include
- int main()
- {
- char arr[] = "hello bit";
- memset(arr,'x',5); //将arr地址后五个字节的内存初始化为‘x’
- printf("%s",arr);
- return 0;
- }
- #include
- #include
- 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;
- }