Use C calloc function to allocate memory for an array

Syntax

C calloc function has the following format. void *calloc(size_t num, size_t size);

C calloc function is from header file stdlib.h.

Description

C calloc function allocates sufficient memory for an array of num objects of size size.

C calloc function returns a pointer to the first byte of the allocated region.

If there is not enough memory to satisfy the request, a null pointer is returned.

All bits in the allocated memory are initially set to zero. It is important to verify that the return value is not null before attempting to use it.

Return a pointer to a dynamically allocated array of 100 floats:

Example

Use C calloc function to allocate memory for an array.


#include <stdlib.h>
#include <stdio.h>
/*from www. java  2 s.  com*/
int main(void){

  float *p;

  p = calloc(100, sizeof(float));
 
  if(!p) {
    printf("Allocation Error\n");
    exit(1);
  }
}




















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h