memset函数源码实现

来源:互联网 发布:java实现bt下载 编辑:程序博客网 时间:2024/04/29 22:38
void * memset(void *dst, int val, size_t count)

将dst所指向的某一块内存中的前count个 字节的内容全部设置为val指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为指向s的指针

简单来说就是把dst所指内存区域的前count个字节设置为val。返回指向dst的指针。

#include<stdio.h>#include<stdlib.h>#include<assert.h>#define N 5void * 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;}

wKioL1ZQPErxnDiDAAAg4L0oR6E653.png

0 0
原创粉丝点击