fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int AddString(char*** strings, size_t* count, const char* newStr)
  6. {
  7. char* copy;
  8. char** p;
  9.  
  10. if (strings == NULL ||
  11. newStr == NULL ||
  12. (copy = malloc(strlen(newStr) + 1)) == NULL)
  13. return 0;
  14.  
  15. strcpy(copy, newStr);
  16.  
  17. if ((p = realloc(*strings, (*count + 1) * sizeof(char*))) == NULL)
  18. {
  19. free(copy);
  20. return 0;
  21. }
  22.  
  23. *strings = p;
  24.  
  25. (*strings)[(*count)++] = copy;
  26.  
  27. return 1;
  28. }
  29.  
  30. void PrintStrings(char** strings, size_t count)
  31. {
  32. printf("BEGIN\n");
  33. if (strings != NULL)
  34. while (count--)
  35. printf(" %s\n", *strings++);
  36. printf("END\n");
  37. }
  38.  
  39. int main(void)
  40. {
  41. char** strings = NULL;
  42. size_t count = 0;
  43. PrintStrings(strings, count);
  44. AddString(&strings, &count, "Hello World!");
  45. PrintStrings(strings, count);
  46. AddString(&strings, &count, "123");
  47. AddString(&strings, &count, "ABCDEF");
  48. PrintStrings(strings, count);
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 1920KB
stdin
Standard input is empty
stdout
BEGIN
END
BEGIN
  Hello World!
END
BEGIN
  Hello World!
  123
  ABCDEF
END