Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: const char ** and char ** incompatibility

Reply
Thread Tools

Re: const char ** and char ** incompatibility

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-19-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> const char *cp; char *p;
> const char **cpp; char **pp;
> why cp = p is ok, but cpp = pp have a warning: pointer incompatible?


The message is a little misleading because all the types involved are
incompatible, but when assigning between pointer types there are two
conditions:

(1) both operands are pointers to qualified or unqualified versions of
compatible types, and

(2) the type pointed to by the left has all the qualifiers of the type
pointed to by the right.

The first assignment meets the both criteria. const char * and char *
are, respectively, pointers to a qualified and an unqualified version of
char, and const char has all the qualifiers of char (plus an extra one).

The second assignment fails the first test. The pointed-to types are
const char * and char * and these are not qualified or unqualified
versions of compatible types. A const-qualified pointer has the const
in a different place: char * const. In const char * the const is
qualifying the char, not the pointer type.

As for the motivation for this rule, it is designed to be able to catch
code that would otherwise modify a const object. The type restriction
is, in fact, overly strict in that it insists on a diagnostic for some
rare but perfectly safe code sequences. I image it was chosen because
it is simpler than trying to specify exactly the lest restrictive rule
that still catches all such code.

--
Ben.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-30-2013
On 12/30/2013 07:50 AM, gyl wrote:
> On Thu, 19 Dec 2013 11:55:03 +0000, Ben Bacarisse
> <(E-Mail Removed)> wrote:

....
>> The message is a little misleading because all the types involved are
>> incompatible, but when assigning between pointer types there are two
>> conditions:
>>
>> (1) both operands are pointers to qualified or unqualified versions of
>> compatible types, and
>>
>> (2) the type pointed to by the left has all the qualifiers of the type
>> pointed to by the right.

....
> Thanks for your explaination, it could be better if you can give some
> references where you took these rules from.


6.5.16.1p1 gives the constraints that apply to simple assignment, and
the case that applies to this code is as follows:

> — the left operand has atomic, qualified, or unqualified pointer type, and (considering
> the type the left operand would have after lvalue conversion) both operands are
> pointers to qualified or unqualified versions of compatible types, and the type pointed
> to by the left has all the qualifiers of the type pointed to by the right;


You can find a draft version of the current C standard is available as a
file named n1570.pdf. It can be obtained from many sources, but the most
authoritative is the committee's own web site:
<http://open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf>. Ironically,
Google searches will not show this site, because a complaint about
copyright infringement has been mistakenly filed with Google, despite
the fact that the web site is maintained on behalf of the copyright
holder. I gather the committee is currently working to get that fixed.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
the difference between "const char* s" and "char* const s" Gary C Programming 9 08-24-2006 09:57 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments