fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char *nome(char text[]) {
  5. int n3 = 0;
  6. int n2 = 0;
  7. char *n = NULL;
  8. while((n2 = getchar()) != '\0' && n2 != EOF) {
  9. n = realloc(n, ++n3);
  10. if (n == NULL) {
  11. puts("Erro ao realocar!");
  12. exit(0);
  13. }
  14. *(n + (n3 - 1)) = n2;
  15. }
  16. *(n + n3) = '\0';
  17. return n;
  18. }
  19.  
  20. int main(void) {
  21. char *name = nome("Nome:");
  22. char *iterador = name;
  23. while(*iterador != '\0') printf("%c", *iterador++);
  24. printf("%s", name); //bem mais simples, certo?
  25. free(name);
  26. }
  27.  
  28. //https://pt.stackoverflow.com/q/213845/101
Success #stdin #stdout 0s 4184KB
stdin
teste
stdout
teste
teste