Topic: Is char** to const char ** illegal?


Author: horstman@sjsumcs.sjsu.edu (Cay Horstmann)
Date: Fri, 11 Feb 1994 02:05:20 GMT
Raw View
In article <1994Feb8.230934.18077@is.morgan.com> jlieb@is.morgan.com (Jerry Liebelson) writes:
>Hi,
>  Does anyone know what the Standard says (or does not say) about the
>following conversion:
>
>        char *s;
>        const char** sp = &s;
>
>  CenterLine 2.0.4 does not report this conversion as being an error, but
>  Lucid 3.1 does.
>
>  Even though there is an additional level of indirection, why should
>  such usage be any less legal than
>
>  char *s;
>  const char *sp = s;
>
>  The assignment is to a more restricted, safer (const-wise) pointer in
>both cases.
>
>--
An article in the C++ Report, about ten months ago, by Andrew Koenig,
explains this very clearly. I believe it is entitled "A const source of
confusion". The conversion char** -> const char** is illegal, char** ->
char* const* is legal (of course), and Andy argues that char** -> const char*
const* should be legal as well. Confusing? Well, not all compiler manufacturers
get this straight either. I tried various versions of Borland, with conflicting
and incorrect results.

Cay
horstman@cs.sjsu.edu






Author: jlieb@is.morgan.com (Jerry Liebelson)
Date: Tue, 8 Feb 1994 23:09:34 GMT
Raw View
Hi,
  Does anyone know what the Standard says (or does not say) about the
following conversion:

        char *s;
        const char** sp = &s;

  CenterLine 2.0.4 does not report this conversion as being an error, but
  Lucid 3.1 does.

  Even though there is an additional level of indirection, why should
  such usage be any less legal than

  char *s;
  const char *sp = s;

  The assignment is to a more restricted, safer (const-wise) pointer in
both cases.

--
Jerry Liebelson
jlieb@is.morgan.com
73477.2740@compuserve.com




Author: matt@x.org (Matt Landau)
Date: 9 Feb 94 00:09:38 GMT
Raw View
jlieb@is.morgan.com (Jerry Liebelson) writes:
>Does anyone know what the Standard says (or does not say) about the
>following conversion:
>
>        char *s;
>        const char** sp = &s;

char ** to const char ** is not a legal conversion.  Here's why:

    void foo ()
    {
        const char c = 'a';
        char *p = 0;
        char **pp = &p;         // points to p
        const char **pcp = pp;  // points to p, which is null
        *pcp = &c;              // makes p point to c
        *p = 'x';               // clobber c
    }

(Thanks to Scott Turner at Liant for the example.)

I believe if you think it through, you find that char ** converts safely
to const char * const *, rather than const char **.

>  CenterLine 2.0.4 does not report this conversion as being an error, but
>  Lucid 3.1 does.

Sounds like a bug in CenterLine's CC (or more probably in cfront).
--
 Matt Landau      Technical Director, Application Toolkits
 matt@x.org      The X Consortium, Inc.

 Waiting for a flash of enlightenment in all this blood and thunder.




Author: jss@summit.lucid.com (Jerry Schwarz)
Date: 09 Feb 1994 01:24:34 GMT
Raw View
In article <1994Feb8.230934.18077@is.morgan.com> jlieb@is.morgan.com (Jerry Liebelson) writes:


>     Does anyone know what the Standard says (or does not say) about the
>   following conversion:
>

There is no standard.  There is no draft standard. There is no
proposed standard.  There is a working paper which has no official
status (yet).  [Just wanted to get that off my chest.  I have seen
numerous posting recently that refered to one of these non-existant
entities.]


>           char *s;
>           const char** sp = &s;
>

The working paper is clear that this is an error.  This conversion is
not safe, as illustrated in the example

        char* s ;
        const char msg[] = "never change"
        const char** sp = &s ;
        *sp = msg ;  // Should be 100% safe
        s[0] = 'N' ;
        assert(msg[0] == 'n') ; // Whoops!!!!!


Lucid 3.0 does implement the full set of conversions allowed by the
const safety rules accepted by the C++ committee.  In particular, it
accepts

        char* s ;
        const char * const * sp = &s ;

which is safe, although it isn't well-formed according to the C
standard.

There is more discussion of these issues in Lucid's technote 40.

  -- Jerry Schwarz(jss@lucid.com)