by Dinesh Thakur

The function is used to allocate contiguous blocks of memory of same size during the execution of the program. So, it may be applied for creating arrays of fundamental types as well as structures. The function malloc ( ) may also be used for the same purpose. The prototype of the function calloc () is given below.

void* calloc( size_t n, size_t m);

In the above code, the first unsigned number n represents the number of memory blocks to be created and the second unsigned number m represents the size of each memory block. As an example, examine the following code:

short* Ptrs = (short*) calloc (5, sizeof(short));

For the above code, function calloc ()allocates 5 blocks of memory each of size two bytes for storing 5 short integer numbers. The return value is assigned to Ptrs . It is the address of the first byte of the first block.

The test for availability of memory as illustrated above for the function malloc () should be

included for this function as well. Thus, for allocating memory for n structures of type students (described

above) the code may be written as below.

struct student *Pst;

Pst = (struct student*) calloc (size_t n, sizeof(struct student));

if( Pst ==NULL)

printf("Error in allocation of memory.");

exit(1);