calloc 分配一个二维储存空间

头文件

#include <stdlib.h>

语法

void *calloc( size_t num, size_t size );

功能

函数返回一个指向 num 数组空间,每一数组元素的大小为 size。如果发生错误,则返回 NULL。

示例

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *str = NULL;

    /* allocate memory for string */
    str = calloc(10, sizeof(char));

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
}

输出结果:

String is Hello
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号