zoukankan      html  css  js  c++  java
  • C库函数-calloc()

    转载:https://www.tutorialspoint.com/c_standard_library/c_function_calloc.htm

     

    描述

    C库函数void * calloc(size_t nitems,size_t size)分配请求的内存并返回指向它的指针。malloccalloc的区别在于malloc不会将内存设置为零,而calloc会将分配的内存设置为零。

    宣言

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

    void *calloc(size_t nitems, size_t size)

    参量

    • nitems-这是要分配的元素数。

    • 大小 -这是元素的大小。

    返回值

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

    以下示例显示calloc()函数的用法。

    #include <stdio.h>
    #include <stdlib.h>
    
    int main () {
       int i, n;
       int *a;
    
       printf("Number of elements to be entered:");
       scanf("%d",&n);
    
       a = (int*)calloc(n, sizeof(int));
       printf("Enter %d numbers:
    ",n);
       for( i=0 ; i < n ; i++ ) {
          scanf("%d",&a[i]);
       }
    
       printf("The numbers entered are: ");
       for( i=0 ; i < n ; i++ ) {
          printf("%d ",a[i]);
       }
       free( a );
       
       return(0);
    }

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

    Number of elements to be entered:3
    Enter 3 numbers:
    22
    55
    14
    The numbers entered are: 22 55 14
  • 相关阅读:
    Next Permutation
    Generate Parentheses
    Unique Binary Search Trees II
    LDP LSP建立
    LDP标签分发和管理
    维护LDP会话
    LDP会话状态机
    LDP会话建立过程
    LDP发现机制
    LDP术语
  • 原文地址:https://www.cnblogs.com/music-liang/p/13163712.html
Copyright © 2011-2022 走看看