#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
//下标法
void* mymemset(void* _Dst, int _Val, unsigned int _Size) {
if (_Dst != NULL) {
return NULL;
}
for (int i = 0; i < _Size; i++) {
((char *)_Dst)[i] = _Val; //下标法
}
return _Dst;
}
//指针法
void* mymemsetaddr(void* _Dst, int _Val, unsigned int _Size) {
if (_Dst != NULL) {
return NULL;
}
//(char *)_Dst) + _Size 注意类型
for (char * p = _Dst; p < ((char *)_Dst) + _Size; p++) {
*p = _Val;//指针法
}
return _Dst;
}
void main() {
int a[5] = { 1,2,3,4,5 };
double db[5] = { 1.1,2.2,3.3,4.4,5.5 };
char str[1024] = "hello world";
mymemsetaddr(a, 0, 20);
mymemsetaddr(db, 0, 40);//整数,实数清零
mymemsetaddr(str, 'A', 1023);
//字符串,数据填充
for (int i = 0; i < 5; i++) {
printf("\n%d,%f", a[i], db[i]);
}
printf("%s", str);
system("pause");
}
memset内存清零及实现
最新推荐文章于 2023-09-07 17:14:39 发布