Pages

Monday, October 22, 2018

Using realloc function

Dynamic memory management - realloc () function

#include <stdio.h>
#include <stdlib.h>

int main () 
{
   char *str;

   /*memory allocation using malloc()*/
   str = (char *) malloc(7);
   
   strcpy(str, "Gujarat");
   printf("Original String = %s\n\n", str);

   /* Reallocating memory using realloc()*/
   str = (char *) realloc(str, 25);
   printf("Reallocation done...\n\n");
   strcat(str, ", India");
   printf("Updated String = %s", str);

   free(str);
   return(0);
}

Output of Program

Original String = Gujarat

Reallocation done...

Updated String = Gujarat, India

No comments:

Post a Comment