Mega Code Archive

 
Categories / C / Code Snippets
 

Store string in allocated memory

#include <stdlib.h> #include <stdio.h> int main(void) { char *str[100]; int j; for(j = 0; j < 100; j++) { if((str[j] = malloc( 128 )) == NULL) { printf("Allocation Error\n"); exit(1); } gets(str[j]); } /* now free the memory */ for(j = 0; j < 100; j++) free(str[j]); return 0; }