Mega Code Archive

 
Categories / C / Code Snippets
 

Allocate memory and reallocate

#include <stdlib.h> #include <stdio.h> #include <string.h> int main(void) { char *j; j = malloc(17); if(!j) { printf("Allocation Error\n"); exit(1); } strcpy(j, "This is 16 chars"); j = realloc(j, 18); if(!j) { printf("Allocation Error\n"); exit(1); } strcat(j, "."); printf(j); free(j); return 0; }