man scanf says you can have %sa for scanf to allocate a buffer of
sufficient size and and assign the address to this buffer to the
argument(which should be a pointer to a char *).

      char *user_name;
      printf("Enter the name for this new user.\n");
       scanf("%sa", &user_name);

On Sun, Dec 4, 2011 at 2:32 PM, Mihir Mehta <[email protected]>wrote:

> Hi all,
>
> I'm programming a terminal-based application with c, and at various
> point, I need to prompt the user for input data. For this, I use code
> like this:
>
>        char user_name[STRING_SIZE];
>        printf("Enter the name for this new user.\n");
>        scanf("%s", user_name);
>
> This works for now, but I feel that having the string size limited
> this way (by the constant STRING_SIZE) could be a problem, and the
> scanf man page suggests that it is capable of dynamically allocating
> enough space to the character array user_name if it is called
> appropriately. However, the language of the man page is somewhat
> unclear on this point, so could someone help me out by letting me know
> what changes I need to make to my code for the desired behaviour?
>
> Thanks,
> Mihir.
>
> --
> Mailing list guidelines and other related articles:
> http://lug-iitd.org/Footer
>



-- 
Tavish Naruka

-- 
Mailing list guidelines and other related articles: http://lug-iitd.org/Footer

Reply via email to