Store string in allocated memory : Memory Allocation « Memory « C / ANSI-C






Store string in allocated memory


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

int main(void)
{
  char *str[100];
  int i;

  for(i = 0; i < 100; i++) {
    if((str[i] = malloc( 128 )) == NULL) {
      printf("Allocation Error\n");
      exit(1);
    }
    gets(str[i]);
  }

  /* now free the memory */
    
  for(i = 0; i < 100; i++) 
      free(str[i]);

  return 0;
}

           
       








Related examples in the same category

1.Find out the address after mallocFind out the address after malloc
2.Allocate memory
3.Allocate memory and reallocateAllocate memory and reallocate
4.Allocate space for a string dynamically, request user input, and then print the string backwardsAllocate space for a string dynamically, request user
   input, and then print the string backwards
5. Allocate memory block: how to use malloc Allocate memory block: how to use malloc
6. Allocate array in memory: how to use calloc Allocate array in memory: how to use calloc
7.Use malloc to allocate memory
8.Get the current system free memory
9. Reallocate memory block: how to use realloc