fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int name(char *table, char *text)
  6. {
  7. table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));
  8.  
  9. if (table == NULL) {
  10. return EXIT_FAILURE;
  11. }
  12.  
  13. /* O nome da tabela é copiado para "table" */
  14. strncpy(table, text, (strlen(text) + 1) * sizeof(char));
  15.  
  16. return EXIT_SUCCESS;
  17. }
  18.  
  19. int add(char *table, char *text)
  20. {
  21. //table = (char *) realloc(table, (strlen(text) + 1) * sizeof(char));
  22. table = (char *) realloc(table, ((strlen(table)) + (strlen(text) + 1)) * sizeof(char));
  23.  
  24. if (table == NULL) {
  25. return EXIT_FAILURE;
  26. }
  27.  
  28. /* Os dados da tabela são concatenados */
  29. strncat(table, text, (strlen(text) + 1) * sizeof(char));
  30.  
  31. return EXIT_SUCCESS;
  32. }
  33.  
  34. int main(void)
  35. {
  36. char *table = (char *) malloc(sizeof(char));
  37.  
  38. if (table == NULL) {
  39. fprintf(stderr, "Erro ao inicializar o ponteiro!\n");
  40. exit(EXIT_FAILURE);
  41. }
  42.  
  43. name(table, "Nomes:\n");
  44.  
  45. add(table, "Lucas\n");
  46. add(table, "Vanessa\n");
  47.  
  48. add(table, "Fulano\n");
  49. add(table, "Ciclano\n");
  50. add(table, "Beltrano\n");
  51. add(table, "Teste 123... 123...\n");
  52.  
  53. printf("%s", table);
  54.  
  55. free(table);
  56.  
  57. return EXIT_SUCCESS;
  58. }
Success #stdin #stdout 0s 5544KB
stdin
Standard input is empty
stdout
Nomes:
Lucas
Vanessa
Fulano
Ciclano
Beltrano
Teste 123... 123...