Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson 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: References: <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 G G 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 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"