Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: double strtod( const char *nPtr, char **endPrt); |
| Date | 2015-11-13 02:07 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87a8qizm6z.fsf@bsb.me.uk> (permalink) |
| References | <fcf0991b-eb10-4594-8745-0ae2fc15721f@googlegroups.com> <31297c7a-2d50-43d7-bbcf-7bff154ebb46@googlegroups.com> <87h9kqzsc8.fsf@bsb.me.uk> <0d5dd3cf-f3bf-4e1e-a61e-d7e8df92453d@googlegroups.com> |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Thursday, November 12, 2015 at 11:55:02 PM UTC, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>> <snip>
>> > Consider this.
>> >
>> >
>> > char line[1024]; /* modifiable */
>> > const char * end;
>> > double x;
>> >
>> > fgets(line, 1024, stdin);
>> > x = strtod(line, &end);
>> > /* we want to null terminate the numerical part of line */
>> > if(end != line)
>> > *end = 0;
>> > /* we can't do that if end is a const char * */
>> >
>> > It illustrates the problems with const.
>>
>> What problem does it illustrate? -- that you've used the wrong type by
>> adding const? Why is that a problem with const? (There are problems
>> with const in C, but I don't see which one you are illustrating here.)
>>
> strtod take a const char *, so the "end" pointer should also
> be a const pointer. Otherwise if you can defeat the const system
> by passing strtod a const string, and writing to the end pointer.
Would that problem not be better illustrated by a program that does
exactly that? To the non-expert, your example just looks like a
misused const since that's the only thing wrong with the code.
<snip>
> But we can't declare the end pointer to be const qualified, because
> if we do that, you can't write through it, which you'll often
> legitimately want to do.
No, that would not a problem:
if (end != line)
line[end-line] = 0;
The problem is that you can "get round" const either accidentally or on
purpose using oddly-defined library functions. Your example didn't
illustrate that.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
double strtod( const char *nPtr, char **endPrt); G G <gdotone@gmail.com> - 2015-11-11 22:55 -0800
Re: double strtod( const char *nPtr, char **endPrt); Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 07:24 +0000
Re: double strtod( const char *nPtr, char **endPrt); G G <gdotone@gmail.com> - 2015-11-11 23:42 -0800
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 08:31 -0800
Re: double strtod( const char *nPtr, char **endPrt); supercat@casperkitty.com - 2015-11-12 08:52 -0800
Re: double strtod( const char *nPtr, char **endPrt); G G <gdotone@gmail.com> - 2015-11-12 09:43 -0800
Re: double strtod( const char *nPtr, char **endPrt); Richard Heathfield <rjh@cpax.org.uk> - 2015-11-12 17:59 +0000
Re: double strtod( const char *nPtr, char **endPrt); Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-12 13:10 -0500
Re: double strtod( const char *nPtr, char **endPrt); James Kuyper <jameskuyper@verizon.net> - 2015-11-12 13:19 -0500
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 11:32 -0800
Re: double strtod( const char *nPtr, char **endPrt); supercat@casperkitty.com - 2015-11-12 12:17 -0800
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 12:48 -0800
Re: double strtod( const char *nPtr, char **endPrt); supercat@casperkitty.com - 2015-11-12 13:39 -0800
Re: double strtod( const char *nPtr, char **endPrt); glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-12 23:24 +0000
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 17:14 -0800
Re: double strtod( const char *nPtr, char **endPrt); supercat@casperkitty.com - 2015-11-12 17:42 -0800
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 18:20 -0800
Re: double strtod( const char *nPtr, char **endPrt); glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-12 23:15 +0000
Re: double strtod( const char *nPtr, char **endPrt); Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-12 18:30 -0500
Re: double strtod( const char *nPtr, char **endPrt); supercat@casperkitty.com - 2015-11-12 16:47 -0800
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 18:07 -0800
Re: double strtod( const char *nPtr, char **endPrt); glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-13 02:41 +0000
Re: double strtod( const char *nPtr, char **endPrt); glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-13 02:53 +0000
Re: double strtod( const char *nPtr, char **endPrt); G G <gdotone@gmail.com> - 2015-11-12 12:17 -0800
Re: double strtod( const char *nPtr, char **endPrt); Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-12 12:34 -0800
Re: double strtod( const char *nPtr, char **endPrt); Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-12 23:54 +0000
Re: double strtod( const char *nPtr, char **endPrt); Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-12 16:23 -0800
Re: double strtod( const char *nPtr, char **endPrt); Ian Collins <ian-news@hotmail.com> - 2015-11-13 14:09 +1300
Re: double strtod( const char *nPtr, char **endPrt); Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-12 17:31 -0800
Re: double strtod( const char *nPtr, char **endPrt); Ian Collins <ian-news@hotmail.com> - 2015-11-13 14:46 +1300
Re: double strtod( const char *nPtr, char **endPrt); Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-13 02:07 +0000
Re: double strtod( const char *nPtr, char **endPrt); David Brown <david.brown@hesbynett.no> - 2015-11-13 08:48 +0100
Re: double strtod( const char *nPtr, char **endPrt); Keith Thompson <kst-u@mib.org> - 2015-11-12 16:57 -0800
csiph-web