1. 编程学习网 > 编程教程 > C语言教程 > C语言库函数-calloc()

C语言库函数-calloc()

C语言标准库 -

C语言库函数 void *calloc(size_t nitems, size_t size) 分配所需的内存空间,并返回一个指向它的指针。malloccalloc 之间的不同点是,malloc 不会设置内存为零,而 calloc 会设置分配的内存为零。

声明

下面是 calloc() 函数的声明。

void *calloc(size_t nitems, size_t size)

参数

  • nitems -- 要被分配的元素个数。
  • size -- 元素的大小。

返回值

该函数返回一个指针,指向已分配的内存。如果请求失败,则返回 NULL。

实例

下面的实例演示了 calloc() 函数的用法。

实例

#include <stdio.h>#include <stdlib.h> int main(){ int i, n; int *a; printf("要输入的元素个数:"); scanf("%d",&n); a = (int*)calloc(n, sizeof(int)); printf("输入 %d 个数字:\n",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("输入的数字为:"); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } free (a); // 释放内存 return(0); }

让我们编译并运行上面的程序,这将产生以下结果:

要输入的元素个数:3
输入 3 个数字:
22
55
14
输入的数字为:22 55 14

C语言标准库 -

本文由IT教学网整理发布,转载请注明出处:http://www.clang.cc//jiaocheng/cyuyan/1366.html

联系我们

在线咨询:点击这里给我发消息

咨询电话:400-998-2681

工作时间:7*24小时无休