The calloc() function allocates memory blocks, we can use it to allocate memory for arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
//Allocating memory dynamically on the heap
//for a character array of 50 characters and
//sets each element to zero.
char* a = (char*) calloc (50, sizeof(char));
if (a != NULL)
printf("Allocated memory for a character array\n");
else
printf("calloc() failed");
//We can use strcpy to copy to this array, since
//we have allocated memory
strcpy(a, "Hello from lautturi");
printf("The character array is:%s\n", a);
//Free the allocated memory
free(a);
return 0;
}ecruoS:www.lautturi.comoutput:
Allocated memory for a character array The character array is:Hello from lautturi Freed memory from the heap