C语言-内存函数

1,memcpy

2,memmove

3,memcmp

4,memset

------------------------------------------------------------------------------------------------------------------

1,memcpy

复制代码
#include<stdio.h>
//memcpy
//内存拷贝函数

//int main() {
//	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
//	int arr2[20] = { 0 };
//	memcpy(arr2, arr1, 20);//注意memcpy的函数参数类型是void    (20字节,对应5个int数  )
//	int i = 0;
//	for ( i = 0; i < 5; i++)
//	{
//		printf("%d ", arr2[i]);
//	}
//
//	return 0;
//}

//模拟实现memcpy
#include<assert.h>
//void* my_memcpy(void* dest, const void* src, size_t num) {
//	assert(dest && src);
//	void* ret = dest;
//	while (num--) {
//		*(char*)dest = *(char*)src;
//		dest = (char*)dest + 1;
//		src = (char*)src + 1;
//
//	}
//	return ret;
//}
//
//void test1() {
//	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
//	int arr2[20] = { 0 };
//	my_memcpy(arr2, arr1, 20);//注意memcpy的函数参数类型是void    (20字节,对应5个int数  )
//	int i = 0;
//	for (i = 0; i < 10; i++)
//	{
//		printf("%d ", arr2[i]);
//	}
//	printf("\n");
//}
#include<string.h>
void test2() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	//my_memcpy(arr1+2, arr1, 20);//注意memcpy的函数参数类型是void    (20字节,对应5个int数  )
	memmove(arr1 + 2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
}
int main() {
	test1();
	test2();

	return 0;
}
//memcpy 负责拷贝两块独立空间中的数据
//重叠内存的拷贝,是怎么做的呢?  ===>memmove

2,memmove

复制代码
//模拟实现memmove
#include<stdio.h>
#include<string.h>
//void* my_memmove(void* dest, const void* src, size_t num) {
	assert(dest && src);
	void* ret = dest;
	if (dest<src)
	{
		//前->后
		while (num--) {
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//后->前
		while (num--) {
			*((char*)dest+num) = *((char*)src+num);
		}
	}
	return ret;
}
void test3() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	//my_memcpy(arr1+2, arr1, 20);//注意memcpy的函数参数类型是void    (20字节,对应5个int数  )
	my_memmove(arr1 , arr1+2, 20);// 将 3,4,5,6,7  移到 1,2,3,4,5
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
}
int main() {
	//test1();
	test3();

	return 0;
}

3,memcmp

复制代码
#include<stdio.h>
//memcmp 
// 
int main() {
	int arr1[] = { 1,2,3,4,5 };//10 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00
	int arr2[] = { 1,3,2 };//01 00 00 00 03 00 00 00 02 00 00 00
	int ret = memcmp(arr1, arr2, 12);
	printf("%d\n", ret);
	return 0;
}

4,memset

复制代码
#include<stdio.h>
//memset
int main() {
	char arr[] = "hello bit";
	memset(arr, 'x', 5);
	//memset(arr+6, 'x', 3);
	printf("%s\n", arr);
	//int arr[10] = { 0 };
	////把arr初始化全为一
	//memset(arr, 1, 40);
	//int i = 0;
	//for ( i = 0; i < 10; i++)
	//{
	//	printf("%d\n", arr[i]);
	//}
	return 0;
}
相关推荐
仰泳的熊猫5 分钟前
题目2308:蓝桥杯2019年第十届省赛真题-旋转
数据结构·c++·算法·蓝桥杯
hssfscv14 分钟前
力扣练习训练2(java)——二叉树的中序遍历、对称二叉树、二叉树的最大深度、买卖股票的最佳时机
java·数据结构·算法
菜鸟‍1 小时前
【后端项目】苍穹外卖day01-开发环境搭建
java·开发语言·spring boot
青槿吖1 小时前
【保姆级教程】Spring事务控制通关指南:XML+注解双版本,避坑指南全奉上
xml·java·开发语言·数据库·sql·spring·mybatis
Yungoal1 小时前
B/S和C/S架构在服务端接收请求
c语言·开发语言·架构
y = xⁿ1 小时前
【LeetCodehot100】二叉树大合集 T94:二叉树的中序遍历 T104:二叉树的最大深度 T226:翻转二叉树 T101:对称二叉树
后端·算法·深度优先
niceffking1 小时前
C++内部类的ISO约定和语法细节
开发语言·c++
不想看见4041 小时前
Search a 2D Matrix II数组--力扣101算法题解笔记
数据结构·算法
IronMurphy1 小时前
【算法二十六】108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
数据结构·算法·leetcode
wjs20241 小时前
C# 常量
开发语言