Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #75868

Re: double strtod( const char *nPtr, char **endPrt);

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: double strtod( const char *nPtr, char **endPrt);
Date Thu, 12 Nov 2015 11:32:57 -0800
Organization None to speak of
Lines 73
Message-ID <ln7flnqahi.fsf@kst-u.example.com> (permalink)
References <fcf0991b-eb10-4594-8745-0ae2fc15721f@googlegroups.com> <lnk2pnqiwk.fsf@kst-u.example.com> <6b8eee47-6184-45f2-8b56-b0e51c16a763@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="20323"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19V/s5GOnTK2lASNHA46fR3"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:DOIoANGQT5hhmwMN5aZgdzSusCE= sha1:jonAoLsvTBeDYV9wlhMyNrjQXIg=
Xref csiph.com comp.lang.c:75868

Show key headers only | View raw


G G <gdotone@gmail.com> writes:
> On Thursday, November 12, 2015 at 11:31:17 AM UTC-5, Keith Thompson wrote:
>> nPtr is a pointer to a const (not constant) character.
>> 
>> A character constant is something like 'x'.  A "constant" is
>> a literal.  A "constant expression" is one that is evaluated at
>> compile time.  (It's not quite that simple, but that's close enough.)
>> 
>> The keyword "const" really means "read-only".  What it means here is
>> that you can't use the pointer nPtr to modify the character that it
>> points to.  The character object itself is not necessarily read-only;
>> you just can't use nPtr to modify it.  (Well, you can if you use a cast,
>> but -- don't do that.)
>
>> The "const" vs. "constant" distinction causes a lot of confusion.
>> Personally, I wish that "const" had been spelled "readonly".
>
> Thanks Keith, my problem, I think stems from the appendix A.2.5.2 in
> C by Kernighan and Ritchie.
>
> A character constant is a sequence of one or more characters 
> enclosed in single quotes as in 'x'. The value of a character 
> constant with only one character is the numeric value of the 
> character inthe machine's character set at execution time. 
> The value of a multi-character constant is implementation-defined.
>
> A string literal, also called a string constant, is a sequence 
> of characters surrounded by double quotes as in "...". A string 
> has type ``array of characters'' and storage class static 
> (see Par.A.3 below) and is initialized with the given characters.
>
> It's stuck in my mind, try and shake it.

That quote from K&R is perfectly accurate; there's nothing to "shake".

The point is that K&R are talking about *constants*, which are lexical
entities that can appear in C source code.  The quoted passage has
doesn't mention the "const" qualifier, and has very little to do with
it.

> const char* nPtr can point to either one of these two types?

Given a declaration
    const char *nPtr;
nPtr points to an object of type char.  (Or it might be a null
pointer that doesn't point to anything, or it might have a garbage
value.)  The "const" places a restriction on the use of the pointer;
you can't use the pointer to modify the object it points to.
It's a safety net, a way to ask the compiler not to let you do
something that might be dangerous.

It can't point to a character constant, because there's no such thing as
a character constant *in a running program*.  'x' is a character
constant, but it exists in that form *only* as a sequence of 3
characters in your ".c" file.  There is no run-time object associated
with it.  Similarly, a pointer of type int* can't point to 42 -- though
it can point to an int object in which you've stored the value 42.

A string literal, unlike a character or integer constant, does cause the
creation of an object at run time.  A char* pointer *can* point to an
element of that object.  (And for safety, such a pointer should be
declared as "const char*", because you want to avoid trying to modify
the contents of the array created by the string literal.)

I hope I haven't caused more confusion.  Please feel free to follow up
if you don't understand what I've written (or set it aside and come back
to it later if you prefer).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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