Dynamic Memory Allocation in C using calloc() Function


In C programming, the calloc() function is used to dynamically allocate memory for an array during runtime, similar to the malloc() function. However, unlike malloc(), calloc() initializes the memory block to all zero bits. The function is defined in the stdlib.h library.
The calloc() function takes two arguments, the number of elements in the array and the size of each element. It then returns a pointer to the first byte of the allocated memory block.
Here is an example of how the calloc() function can be used in C:
      Syntax :
             void* calloc ( size_t n , size_t size )
      Example :
             int *ptr = int , calloc ( 5 , size ( int ) )

  • This program demonstrates the use of the calloc() function in C to dynamically allocate memory for an array during runtime. The program first prompts the user to enter the limit of the array and then uses the calloc function to allocate memory for n integers.
  • The calloc function takes two arguments, the number of elements in the array (n) and the size of each element (sizeof(int)), and returns a pointer to the first byte of the allocated memory block. The program then checks if the memory allocation is successful by checking if the pointer returned by calloc is NULL. If it is, the program exits and displays a message "Memory Not Available...".
  • The program then prompts the user to enter n integers and assigns them to the elements of the array using a for loop. The program then uses another for loop to print the address and value of each element of the array using the pointer arithmetic.
  • In summary, the program demonstrates how to use the calloc function to dynamically allocate memory for an array, how to assign values to the elements of the array using pointer arithmetic, and how to print the address and value of each element of the array using a pointer.

Source Code

//calloc in Pointers
#include<stdio.h>
#include<stdlib.h>
int main()
{
    //void* calloc(size_t n,size_t size)
 
    int i,n;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);
    int *ptr=(int *)calloc(n,sizeof(int));
 
    if(ptr==NULL)
    {
        printf("Memory Not Available ...");
        exit(1);
    }
 
    for(i=0; i<n; i++)
    {
        printf("Enter a integer : ");
        scanf("%d",ptr+i);
    }
 
    for(i=0; i<n; i++)
    {
        printf("\n%d :  %d  ",&ptr+i,*(ptr+i));
    }
 
    return 0;
}
 
 
 
 
To download raw file Click Here

Output

Enter The Limit : 5
Enter a integer : 1
Enter a integer : 2
Enter a integer : 3
Enter a integer : 4
Enter a integer : 5

6356724 :  1
6356728 :  2
6356732 :  3
6356736 :  4
6356740 :  5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C