《C语言疑难点 --- C语内存函数专题》

《C语言疑难点 --- 字符函数和字符串函数专题(上)》


🔥小龙报:个人主页

🎬作者简介:C++研发,嵌入式,机器人方向学习者

❄️个人专栏:《C语言》《算法》KelpBar海带Linux智慧屏项目

✨永远相信美好的事情即将发生

文章目录



一、memcpy使用和模拟实现

函数链接:memcpy

1.1函数的解析与使用

c 复制代码
void * memcpy ( void * destination, const void * source, size_t num );

函数解析:

• 函数memcpy从source的位置开始向后复制num个字节 的数据到destination指向的内存位置。

• 这个函数在遇到 '\0' 的时候并不会停下来。

• 如果source和destination有任何的重叠,复制的结果都是未定义的 。

例:

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };

	memcpy(arr2, arr1, 20);

	for (int i = 0; i < 10; i++)
		printf("%d ",arr2[i]);
	return 0;
}

运行结果:

1.2函数的模拟实现

c 复制代码
#include <stdio.h>
#include <assert.h>

void* my_memcpy(void* dest, const void* str, size_t num)
{
	assert(dest);
	assert(str);
	while (num--)
	{
		*(char*)dest = *(char*)str;
		dest = (char*)dest + 1;
		str = (char*)str + 1;
	}
}

int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };


	my_memcpy(arr2, arr1, 20);

	for (int i = 0; i < 10; i++)
		printf("%d ",arr2[i]);
	return 0;
}

运行结果:

二、memmove使用和模拟实现

函数链接:memmove

2.1函数的解析与使用

c 复制代码
void * memmove ( void * destination, const void * source, size_t num );

函数解析:

• 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。

• 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

例:

c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };

	memmove(arr1 + 2, arr1, 20);

	for (int i = 0; i < 10; i++)
		printf("%d ",arr1[i]);
	return 0;
}

运行结果:

2.2函数的模拟实现

c 复制代码
#include <stdio.h>
#include <string.h>
#include <assert.h>

void* my_memmove(void* dest, const void* str, size_t count)
{
	assert(dest && str);

	if (dest < str || (char*)dest >= (char*)str + count)
	{
		*(char*)dest = *(char*)str;
		dest = (char*)dest + 1;
		str = (char*)str + 1;
		while (count--)
		{
			*(char*)dest = *(char*)str;
			dest = (char*)dest + 1;
			str = (char*)str + 1;
		}

	}
	else
	{
		dest = (char*)dest + count - 1;
		str = (char*)str + count - 1;
		while (count--)
		{
			*(char*)dest = *(char*)str;
			dest = (char*)dest - 1;
			str = (char*)str - 1;
		}
	}
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr1 + 2, arr1, 20);


	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

运行结果:

三、memset函数的使用

函数链接:memset

3.1函数的解析与使用

c 复制代码
void * memset ( void * ptr, int value, size_t num );

函数解析:

memset是⽤来设置内存的,将内存中的值以字节为单位设置成想要的内容。

例:

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "hello world";

	memset(str, 'x', 6);
	
	printf("%s",str);
	return 0;
}

运行结果:

四、memcmp函数的使用

函数链接:memcmp

4.1函数的解析与使用

c 复制代码
int memcmp ( const void * ptr1, const void * ptr2, size_t num );

函数解析:
比较从ptr1和ptr2指针指向的位置开始,向后的num个字节

例:

c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
	char str1[] = "abcdefgh";
	char str2[] = "abchigkl";
	int n;
	n = memcmp(str1, str2, sizeof(str1));
	if (n > 0)
		printf("'%s' is greater than '%s'.\n", str1, str2);
	else if (n < 0)
		printf("'%s' is less than '%s'.\n", str1, str2);
	else
		printf("'%s' is the same as '%s'.\n", str1, str2);
	
	return 0;
}

运行结果:

总结与每日励志时刻

这篇C语言教程重点讲解了字符和字符串处理相关的内存操作函数 ,包括memcpy、memmove、memset和memcmp四个函数。文章详细解析了每个函数的参数、功能特点和使用方法,并通过实例代码演示了其具体应用。特别对memcpy和memmove函数进行了模拟实现,展示了底层实现逻辑。memcpy用于不重叠内存区域的复制,memmove可处理重叠区域,memset用于内存设置,memcmp则比较内存区域内容。文章还强调了这些函数与字符串函数的关键区别:它们按字节操作且不受空字符'\0'影响。

相关推荐
晓蛋3 天前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
一隅论数智3 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿3 天前
重载、重写(覆盖)、重定义区别
c语言
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
phltxy3 天前
C 语言指针:从内存地址到灵活的数据访问
c语言
liufeiyu19763 天前
VS 2026 升级后出现错误: 找不到指定的 SDK“Microsoft.NET.Sdk”
visual studio·vs 2026