Example 1: malloc() and free()
This program calculates the sum of n numbers entered by the user. To perform this task,
memory is dynamically allocated using malloc(), and memory is freed using free()
function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n, i, *ptr, sum = 0;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    ptr = (int*) malloc(n * sizeof(int));
    if(ptr == NULL)
    {
         printf("Error! memory not allocated.");
         exit(0);
    }
    printf("Enter elements: ");
    for(i = 0; i < n; ++i)
    {
         scanf("%d", ptr + i);
         sum += *(ptr + i);
    }
    printf("Sum = %d", sum);
    free(ptr);
    return 0;
}
Example: Find Largest Element Using Dynamic Memory
Allocation - calloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i, num;
    float *data;
    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &num);
    // Allocates the memory for 'num' elements.
    data = (float*) calloc(num, sizeof(float));
    if(data == NULL)
    {
        printf("Error!!! memory not allocated.");
        exit(0);
    }
    printf("\n");
    // Stores the number entered by the user.
    for(i = 0; i < num; ++i)
    {
        printf("Enter Number %d: ", i + 1);
        scanf("%f", data + i);
   }
   // Loop to store largest number at address data
   for(i = 1; i < num; ++i)
   {
       // Change < to > if you want to find the smallest number
       if(*data < *(data + i))
          *data = *(data + i);
   }
/*C program to read and print the student details
using structure and Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
/*structure declaration*/
struct student
{
    char name[30];
    int roll;
    float perc;
};
int main()
{
    struct student *pstd;
    /*Allocate memory dynamically*/
    pstd=(struct student*)malloc(1*sizeof(struct
student));
   if(pstd==NULL)
   {
       printf("Insufficient Memory, Exiting... \n");
       return 0;
   }
   /*read and print details*/
   printf("Enter name: ");
         gets(pstd->name);
         printf("Enter roll number: ");
         scanf("%d",&pstd->roll);
         printf("Enter percentage: ");
         scanf("%f",&pstd->perc);
    printf("\nEntered details are:\n");
    printf("Name: %s, Roll Number: %d, Percentage:
%.2f\n",pstd->name,pstd->roll,pstd->perc);
         return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
    int n;
    char *text;
    printf("Enter limit of the text: ");
    scanf("%d",&n);
    /*allocate memory dynamically*/
    text=(char*)calloc(n,sizeof(char));
    printf("Enter text: ");
    //scanf(" "); /*clear input buffer*/
    gets(text);
    printf("Inputted text is: %s\n",text);
    /*Free Memory*/
    free(text);
    return 0;
/*C program to read and print the N student
details using structure and Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
/*structure declaration*/
struct student
{
    char name[30];
    int roll;
    float perc;
};
int main()
{
    struct student *pstd;
    int n,i;
        printf("Enter total number of elements: ");
        scanf("%d",&n);
    /*Allocate memory dynamically for n objetcs*/
    pstd=(struct student*)malloc(n*sizeof(struct
student));
        if(pstd==NULL)
        {
            printf("Insufficient Memory, Exiting... \n");
            return 0;
        }
        /*read and print details*/
        for(i=0; i<n; i++)
        {
            printf("\nEnter detail of student [%3d]:\n",i+1);
            printf("Enter name: ");
            scanf(" "); /*clear input buffer*/
            gets((pstd+i)->name);
            printf("Enter roll number: ");
            scanf("%d",&(pstd+i)->roll);
            printf("Enter percentage: ");
        scanf("%f",&(pstd+i)->perc);
    }
    printf("\nEntered details are:\n");
    for(i=0; i<n; i++)
    {
        printf("%30s \t %5d \t %.2f\n",(pstd+i)-
>name,(pstd+i)->roll,(pstd+i)->perc);
    }
    return 0;
}