Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
53 lines (45 loc) · 1.62 KB

File metadata and controls

53 lines (45 loc) · 1.62 KB
Copy raw file
Download raw file
Edit and raw actions

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>

int main()
{
    /*
    // 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);

    return 0;
}

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

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.