malloc (Memory Allocation)
used to allocate a single large block of contiguous memory dynamically with specified size.
Â
syntax:
void *malloc(unsigned int n);
Â
where n is unsigned int.
Â
it will return a void pointer that can be cast to any datatype if successful. If unsuccessful, it will return NULL.
Â
Eg: int *ptr=(int *)malloc(4);
Â
//Example Program
#include<stdio.h>
Â
void main()
{
   int i,n;
   printf(“Ener the number of integers:”);
   scanf(“%d”,&n);
   int *ptr=(int *)malloc(n*sizeof(int));
Â
   if(ptr==NULL)
   {
       printf(“Memory not allocated”);
       exit(1);
   }
   for(i=0;i<n;i++)
   {
       printf(“Enter an integer:”);
       scanf(“%d”,ptr+i);
   }
   for(i=0;i<n;i++)
   {
       printf(“%d “,*(ptr+i));
   }
}