Skip to content

Latest commit

 

History

History
53 lines (45 loc) · 1.62 KB

43.calloc函数.md

File metadata and controls

53 lines (45 loc) · 1.62 KB

calloc函数

函数声明void *calloc(size_t nmemb, size_t size);
所在文件stdlib.h
函数功能申请堆内存空间并返回,所申请的空间,自动清零
参数及返回解析
参数size_t nmemb 所需内存单元数量
参数size_t size 内存单元字节数量
返回值void * 成功返回非空指针指向申请的空间 ,失败返回 NULL
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain() { /* // 1.申请3块4个字节存储空间 int *p = (int *)malloc(sizeof(int) * 3); // 2.使用申请好的3块存储空间 p[0] = 1; p[1] = 3; p[2] = 5; printf("p[0] = %i\n", p[0]); printf("p[1] = %i\n", p[1]); printf("p[2] = %i\n", p[2]); // 3.释放空间 free(p); */// 1.申请3块4个字节存储空间int*p=calloc(3, sizeof(int)); // 2.使用申请好的3块存储空间p[0] =1; p[1] =3; p[2] =5; printf("p[0] = %i\n", p[0]); printf("p[1] = %i\n", p[1]); printf("p[2] = %i\n", p[2]); // 3.释放空间free(p); return0; }

最后,如果有任何疑问,请加微信 leader_fengy 拉你进学习交流群。

开源不易,码字不易,如果觉得有价值,欢迎分享支持。

close