初始化内存空间
- mem开头的函数,处理内存空间
- memset 使用一个常量字节填充内存空间
- memset(p,0,N*sizeof(int));
- 指定初始化的内存块p ,指定初始化的常量为0,指定内存块的尺寸N*sizeof(int) //N为常量
- memcpy 拷贝内存空间
- memcpy(p2,p1,n);
- 将p1中的n个数据拷贝到p2中
- memmove 拷贝内存空间
- memcmp 比较内存空间
- memchr 在内存空间中搜索一个字符
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
int main(){
int *p=NULL;
int i;
p=(int*)malloc(N*sizeof(int));
if(p==NULL){
printf("分配内存失败!\n");
exit(1);
}
memset(p,0,N*sizeof(int));
for(i=0;i<N;i++){
printf("%d ",p[i]);
}
putchar('\n');
free(p);
return 0;
}
结果:
0 0 0 0 0 0 0 0 0 0