c语言memset函数_用C编写自己的memset()函数

本文介绍了C语言中memset()函数的作用,用于填充内存块,并展示了如何编写自己的memset()函数。通过示例解释了如何初始化字符数组,并填充不同值。

c语言memset函数

As we have discussed in the previous post that memset() is a library function of “string.h” and it is used to fill the memory blocks with a particular value.

正如我们在前一篇文章中讨论的那样, memset()是“ string.h”的库函数,它用于用特定值填充内存块。

Read more: memset() function in C

阅读更多: C中的memset()函数

Here, we are going to create our own "memset()" function...

在这里,我们将创建自己的“ memset()”函数 ...

Function prototype:

功能原型:

    void myMemSet(void* str, char ch, size_t n);

It will fill n blocks of the str with ch.

它将用ch填充str的 n个块。

Function definition:

功能定义:

//memset() function implemention
//function name: myMemSet()
void myMemSet(void* str, char ch, size_t n){
	int i;
	//type cast the str from void* to char*
	char *s = (char*) str;
	//fill "n" elements/blocks with ch
	for(i=0; i<n; i++)
		s[i]=ch;
}

Example:

例:

#include <stdio.h>
#include <string.h>
#define LEN 10

//memset() function implemention
//function name: myMemSet()
void myMemSet(void* str, char ch, size_t n){
	int i;
	//type cast the str from void* to char*
	char *s = (char*) str;
	//fill "n" elements/blocks with ch
	for(i=0; i<n; i++)
		s[i]=ch;
}

int main(void) {
	char arr[LEN];
	int loop;

	printf("Array elements are (before myMemSet()): \n");
	for(loop=0; loop<LEN; loop++)
		printf("%d ",arr[loop]);
	printf("\n");

	//filling all blocks with 0
	myMemSet(arr,0,LEN);
	printf("Array elements are (after myMemSet()): \n");
	for(loop=0; loop<LEN; loop++)
		printf("%d ",arr[loop]);
	printf("\n");

	//filling first 3 blocks with -1
	//and second 3 blocks with -2
	//and then 3 blocks with -3
	myMemSet(arr,-1,3);
	myMemSet(arr+3,-2,3);
	myMemSet(arr+6,-3,3);
	printf("Array elements are (after myMemSet()): \n");
	for(loop=0; loop<LEN; loop++)
		printf("%d ",arr[loop]);
	printf("\n");

	return 0;
}

Output

输出量

Array elements are (before myMemSet()):
64 86 -85 42 -4 127 0 0 0 0
Array elements are (after myMemSet()):
0 0 0 0 0 0 0 0 0 0
Array elements are (after myMemSet()):
-1 -1 -1 -2 -2 -2 -3 -3 -3 0

Explanation:

说明:

In this example, we declared character array arr of LEN bytes (LEN is a macro with the value 10), when we printed the value of arr, the output is garbage because array is uninitialized. Then, we used myMemSet() and filled all elements by 0. Then, printed the elements again the value of all elements were 0. Then, we filled first 3 elements with -1 and next 3 elements with -2 and next 3 elements with -3. Thus the values of all elements at the end: -1 -1 -1 -2-2 -2 -3 -3 -3 0.

在此示例中,我们声明字符数组arr为LEN字节( LEN是值为10的宏),当我们打印arr的值时,输出是垃圾,因为数组未初始化。 然后,我们使用myMemSet()并用0填充所有元素。 然后,再次打印元素,所有元素的值为0 。 然后,我们用-1填充前3个元素,用-2填充下3个元素,用-3填充下3个元素。 因此,最后所有元素的值: -1 -1 -1 -2-2 -2 -3 -3 -3 0

翻译自: https://www.includehelp.com/c-programs/write-your-own-memset-function-in-c.aspx

c语言memset函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值