Re: const char ** and char ** incompatibility

Discussion in 'C Programming' started by Ben Bacarisse, Dec 19, 2013.

  1. 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.
    Ben Bacarisse, Dec 19, 2013
    #1
    1. Advertising

  2. Ben Bacarisse

    James Kuyper Guest

    On 12/30/2013 07:50 AM, gyl wrote:
    > On Thu, 19 Dec 2013 11:55:03 +0000, Ben Bacarisse
    > <> 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
    James Kuyper, Dec 30, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    808
    Netocrat
    Oct 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,010
    Ian Collins
    May 9, 2006
  3. Javier
    Replies:
    2
    Views:
    544
    James Kanze
    Sep 4, 2007
  4. Edward A. Falk

    Re: const char ** and char ** incompatibility

    Edward A. Falk, Dec 19, 2013, in forum: C Programming
    Replies:
    0
    Views:
    94
    Edward A. Falk
    Dec 19, 2013
  5. Paul N
    Replies:
    0
    Views:
    83
    Paul N
    Dec 30, 2013
Loading...

Share This Page