Re: Proper way to input a dynamically-allocated string



websnarf@xxxxxxxxx said:

> Michel Rouzic wrote:
>> I know it must sound like a newbie question, but I never really had to
>> bother with that before,
>
> Its not. Even experienced programmers seem not to know the proper
> answer to this question (hint:fgets() is hardly adequate.)

In my experience, you're wrong; how to do this is common knowledge amongst
experienced programmers.

>> I'd like to know what's the really proper way for input a string in an
>> array of char that's dynamically allocated. I mean, I wish not to see
>> any such things as char mystring[100]; I don't want to see any number
>> (if possible) I just want to declare something like char *mystring; and
>> then I don't know how allocate it with just as many chars (with the
>> space for the \0 of course) as you get from stdin.
>
> You have to understand, this is a foreign concept to many if not most
> of the readers of this newsgroup. Every string container must have a
> size, and "the C way" is to declare that size up front. You can search
> the archives of this newsgroup to endless examples of this.

Most people who ask questions here are newbies, which is why they're asking
questions; that's why we tend to give them simple answers. Nevertheless,
the "how do I get an entire line of input" thing has been asked and
satisfactorily answered many times here.

> The C
> library is almost completely useless on this issue as well.

That's like saying the toolkit you get with a new bicycle is useless. Well,
yes, it's not brilliant - but it's probably enough to get you up and
rolling on Christmas Day. Serious users will want better in due course, and
quite a few solutions to this problem have been presented in this newsgroup
in the past.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: Running App From Boot
    ... I'm not sure how big of a newbie question this is, ... "kiosk mode". ... So you will find useful info if you use google ... including a probably more appropriate newsgroup. ...
    (microsoft.public.pocketpc)
  • It pays to be experienced
    ... Plenty of incentive for experienced programmers here. ... Jim Griffith. ... This newsgroup posts former jokes from the newsgroup ...
    (rec.humor.funny.reruns)
  • Re: The chart of comp.lang.c
    ... -new- newsgroup, discuss it a bit, and when the rough edges have ... submit it for new newsgroup consideration. ... experienced programmers might offer some advice on an IDE to a C nOOb. ...
    (comp.lang.c)
  • Re: Newbie question: Why Fortran?
    ... bud, you're wrong, but I'm sure you're used to that. ... > I've been a regular poster to comp.lang.c for some time, ... that would be very topical in that newsgroup. ... Just another newbie question ...
    (comp.lang.fortran)
  • Re: Newbie question: Why Fortran?
    ... bud, you're wrong, but I'm sure you're used to that. ... > I've been a regular poster to comp.lang.c for some time, ... that would be very topical in that newsgroup. ... Just another newbie question ...
    (comp.lang.cpp)