calloc

 

Contents

Description

calloc function is used to allocate an array in memory with elements initialized to 0.

Syntax

void * calloc( size_t nElements, size_t nSize )

Parameters

nElements
[input] Number of elements
nSize
[input] Length in bytes of each element

Return

returns a pointer to the allocated space, or NULL if there is insufficient memory available.

Examples

EX1

void    calloc_ex1()
{
    int            *pn;
    size_t         nNumerOfIntegersToAllocate = 200;
    pn = (int*)calloc(nNumerOfIntegersToAllocate, sizeof(int));
    if (pn != NULL)
        out_str("Allocation succesful");
    else
        out_str("Allocation failed!");
    
    // free the allocated memory:
    free(pn);
}

Remark

See Also

malloc

header to Include

origin.h

Reference