C library functions -calloc()
C Standard Library - <stdlib.h>
Description
C Library Functionsvoid *calloc(size_t nitems, size_t size)Allocate the required memory space and return a pointer to it.mallocandcallocThe difference is that malloc does not set the memory to zero, whereas calloc sets the allocated memory to zero.
Note:The calloc() function initializes all allocated memory to zero. If initialization is not needed, the malloc() function can be used instead. Additionally, when using calloc(), note that if the allocated memory block is too large, it may lead to insufficient memory issues.
Declaration
Below is the declaration of the calloc() function.
void *calloc(size_t nitems, size_t size)
Parameter
- nitems-- The number of elements to be allocated.
- size-- The size of each element.
Return Value
This function returns a pointer to the allocated memory. If the request fails, it returns NULL.
Example
The following example demonstrates the usage of the calloc() function.
Example
Let us compile and run the above program, which will produce the following result:
要输入的元素个数:3 输入 3 个数字: 22 55 14 输入的数字为:22 55 14other extensions