#include #include char *mygetline(void); int main() { char *p; printf("Please type one line and press RETURN.\n"); p = mygetline(); printf("%s\n", p); free(p); return EXIT_SUCCESS; } /* Let the user input a line of any length. Allocate a block of memory for the line, excluding the RETURN that the user typed to terminate it. Store the line in the block, including a terminating '\0', and return the address of the first char of the block. */ char *mygetline(void) { size_t i = 0; char *p; char *q; int c; p = malloc(++i); if (p == NULL) { printf("Can't allocate %u bytes.\n", i); exit(EXIT_FAILURE); } while ((c = getchar()) != EOF && c != '\n') { p[i-1] = c; q = realloc(p, ++i); if (q == NULL) { printf("Can't allocate %u bytes.\n", i); exit(EXIT_FAILURE); } p = q; } p[i-1] = '\0'; return p; }