--- In [email protected], "Brett McCoy" <[EMAIL PROTECTED]> wrote:
>
> you need to allocate memory for char
> *input. And as I said before, buf[] is unnecessary. Create a #define
> like BUFSZ 10 and use that to allocate the memory using malloc, and
> remember your buffer needs to be BUFSZ + 1 to hold the terminating
> '\0'.

Just wondering why you need to use malloc()? Eg.

#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT_SZ 10

int main(void)
{
    char buf[MAX_INPUT_SZ + 2]; /* allow for \n and \0 */

    if (!fgets(buf, sizeof buf, stdin))
    {
        printf("input error\n");
        exit(EXIT_FAILURE);
    }

    /* NB. \n added by fgets so no need for it here. */

    printf("you entered: %s", buf);

    exit(EXIT_SUCCESS);
}

Doesn't do any verification though...

John

Reply via email to