Re: Proper way to input a dynamically-allocated string
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Sat, 10 Dec 2005 09:11:01 +0000 (UTC)
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)
.
- References:
- Proper way to input a dynamically-allocated string
- From: Michel Rouzic
- Re: Proper way to input a dynamically-allocated string
- From: websnarf
- Proper way to input a dynamically-allocated string
- Prev by Date: Re: Complicated linking problem
- Next by Date: Re: Invalid read of size 1
- Previous by thread: Re: Proper way to input a dynamically-allocated string
- Next by thread: Re: Proper way to input a dynamically-allocated string
- Index(es):
Relevant Pages
|