calloc


calloc

Allocates memory for an array

 #include <stdlib.h> void *calloc ( size_t n , size_t size  ); 

The calloc( ) function obtains a block of memory from the operating system that is large enough to hold an array of n elements of size size.

If successful, calloc( ) returns a void pointer to the beginning of the memory block obtained. void pointers are converted automatically to another pointer on assignment, so that you do not need to use an explicit cast, although you may want do so for the sake of clarity. If no memory block of the requested size is available, the function returns a null pointer. Unlike malloc( ), calloc( ) initializes every byte of the block allocated with the value 0.

Example

 size_t n; int *p; printf("\nHow many integers do you want to enter? "); scanf("%u", &n); p = (int *)calloc(n, sizeof(int)); /* Allocate some memory */ if (p == NULL)   printf("\nInsufficient memory."); else   /* read integers into array elements ... */ 

See Also

malloc( ), realloc( ); free( ), memset( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net