c语言内存函数(memcpy/memmove/memset/memcmp)详解

目录

一.什么是内存函数?

二:内存函数

1.memcpy

2.memmove函数

3.memset函数

4.memcmp函数


一.什么是内存函数?

内存函数是指对内存空间块的数据进行操作的函数,都在string.h这个文件里。、

二:内存函数

1.memcpy

memcpy:C和C++使用的内存拷贝函数

函数原型:void *memcpy(void *destin, void *source, unsigned n);

函数的功能:从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中,即从源source中拷贝n个字节到目标in中。

注:如果source与destin有任何重叠,则复制的结果是未定义的

参数

  • destin-- 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。

  • source-- 指向要复制的数据源,类型强制转换为 void* 指针。

  • n-- 要被复制的字节数。

返回值

该函数返回一个指向目标存储区destin的指针。

1.memcpy使用实例

#include<stdio.h>
#include<assert.h>
#include<string.h>
int main()
{
	int a = 0;
	int arr1[] = { 1,2,3,4,5,6 };
	int arr2[20] = {0};
	memcpy(arr2, arr1, 20);
	for (int i = 0; i <10; i++)
	{
		printf("%d", arr2[i]);
	}

	return 0;
}

 memcpy模拟实现

#include<stdio.h>
#include<assert.h>
#include<string.h>
void* my_memcpy(void* dst,const void* source, size_t sz)
{
	assert(dst && source);
	void* pa = dst;
	while (sz--)
	{
		*(char*)dst = *(char*)source;//依次交换一个字节
		dst = (char*)dst + 1;
	    source= (char*)source + 1;
	}
	return pa;
}
int main()
{
	int a = 0;
	int arr1[] = { 1,2,3,4,5,6 };
	int arr2[20] = {0};
	my_memcpy(arr2, arr1, 20);//20是字节数
	for (int i = 0; i <10; i++)
	{
		printf("%d", arr2[i]);
	}

	return 0;
}

2.memmove函数

功能:用于拷贝字节,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,但复制后源内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同。

原型:void *memmove( void* dest, const void* src, size_t count );

头文件:<string.h>

1.memmove函数使用:

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr + 3, arr, 20);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}

2.memmove函数模拟实现:

#include<stdio.h>
#include<assert.h>
#include<string.h>
void* my_memmove(void* dst, const void* source, size_t sz)
{
	assert(dst && source);
	void* pa = dst;
		if (dst > source)//如果dst的起始地址比source的地址高,则由source的最后一个字节开始,依次与dst的最后一个字节交换
		{
			while (sz--)
			{
				*((char*)dst + sz) = *((char*)source + sz);
			}
		}
		else//否则从source的第一个字节开始,依次与dst的第一个字节交换
		{
			while (sz--)
			{
				*(char*)dst = *(char*)source;
				dst = (char*)dst + 1;
				source = (char*)source + 1;
			}
		}
       return pa;
}
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	void* p = my_memmove(arr + 3, arr, 20);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}

3.memset函数

memset是计算机中C/C++语言初始化函数。作用是将某一块内存中的内容全部设置为指定的值(以字节为单位), 这个函数通常为新申请的内存做初始化工作。

函数原型:void *memset(void *s, int ch, size_t n);

  memset函数使用:

#include<stdio.h>
#include<string.h>
int main()
{
	char arr[] = "abcdef";
	memset(arr, 'p', 5);
	printf(arr);

	return 0;
}

4.memcmp函数

memcmp函数其功能是把存储区 str1 和存储区 str2 的前 n 个字节进行比较。该函数是按字节比较的,位于string.h。

函数原型

int memcmp(const void *str1, const void *str2, size_t n)

返回值

  • 如果返回值 < 0,则表示 str1 小于 str2。

  • 如果返回值 > 0,则表示 str2 小于 str1。

  • 如果返回值 = 0,则表示 str1 等于 str2。

memcmp函数使用:

#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[] = "sajhjdlsajd";
	char arr2[] = "usaydajd";
	int a=memcmp(arr1, arr2, 5);
	if (a > 0)
	{
		printf("arr1>arr2");
	}
	else if (a = 0)
	{
		printf("arr1=arr2");
	}
	else
	{
		printf("arr1<arr2");
	}


	return 0;
}

完。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值