thespeedracer38

Dynamic Array of Strings

May 11th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #define MAX_BUFFER_CAP 1024
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. int main(void)
  7. {
  8.     char (*names)[MAX_BUFFER_CAP] = NULL;
  9.     int size, i;
  10.     clrscr();
  11.     printf("How many names do you want to store? ");
  12.     scanf("%d", &size);
  13.     fflush(stdin);
  14.     names = malloc(size * sizeof(char*));
  15.     for(i = 0; i < size; i++)
  16.     {
  17.         printf("Enter name %d: ", i+1);
  18.         gets(names[i]);
  19.     }
  20.     printf("The names are\n");
  21.     for(i = 0; i < size; i++)
  22.     {
  23.         printf("%s\n", names[i]);
  24.     }
  25.     free(names);
  26.     getch();
  27.     return 0;
  28. }
Add Comment
Please, Sign In to add comment