#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define N 5
// dst所指向的某一块内存中的前count个 字节的内容全部设置为val指定的ASCII值,
// 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为指向s的指针。
void * my_memset(void *dst, int val, size_t count)
{
//把val传给*dst时两个变量类型要相同,需要用到强制类型转换
assert(dst); //这里需要检验dst的有效性
char* ret = (char*)dst;
while (count--)
{
*ret++ = (char)val;
}
return dst;
}
int main()
{
int arr[N];
int i ;
my_memset(arr,0,N*sizeof(int));
for (i = 0; i < N; i++)
{
printf("%d\n", arr[i]);
}
system("pause");
return 0;
}
memset实现原理
最新推荐文章于 2024-09-03 08:55:55 发布