Advertisement
irmantas_radavicius

Untitled

Oct 25th, 2021
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.     char **data = NULL;
  8.     unsigned dataSize = 0;
  9.     while(1){
  10.         char buffer[256] = { 0 };
  11.         printf("Enter string: ");
  12.         scanf("%255[^\n]", buffer);
  13.         getchar();     
  14.         data = realloc(data, (++dataSize)*sizeof(char *)); 
  15.         data[dataSize-1] = calloc(strlen(buffer)+1, 1);
  16.         strcpy(data[dataSize-1], buffer);
  17.         if (strcmp(buffer, "0") == 0){
  18.             break;
  19.         }
  20.     }
  21.  
  22.    for(int i = 0; i < dataSize; ++i){
  23.     if (strlen(data[i]) > 0)
  24.         printf("[%d] -> %s\n", i, data[i]);
  25.     else
  26.         printf("[%d] empty\n", i);
  27.    }
  28.    for(int i = 0; i < dataSize; ++i){
  29.         free(data[i]);
  30.    }
  31.    free(data);
  32.    
  33.    return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement