C examples for Memory:realloc
Using realloc() to change memory allocation.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main( void ) { char buf[80], *message; puts("Enter a line of text."); gets_s(buf);//from www . ja v a 2 s.c om /* Allocate the initial block and copy the string to it. */ message = (char *)realloc(NULL, strlen(buf)+1); strcpy(message, buf); puts(message); puts("Enter another line of text."); gets_s(buf); /* Increase the allocation, then concatenate the string to it. */ message = (char *)realloc(message,(strlen(message) + strlen(buf)+1)); strcat(message, buf); puts(message); return 0; }