Topic: (char **) versus (const char**) and (const char* const*)


Author: "Boris D. Dimitrov" <boris@caltech.edu>
Date: 1998/11/05
Raw View
void u(const char*       *X);

void w(const char* const *Y) {
  u(Y);                             // let a = "this is an error"
}

void v(      char*       *Z) {
  u(Z);                             // let b = "this is an error"
  w(Z);                             // let c = "this is an error"
}

/*
** Passing a pointer to a const object (Y) as a pointer to a non-const
** object (X) clearly violates the constness of *Y. Therefore a==true.
**
** The DEC, SGI, and GNU compilers report that b==true and c==false.
** Is this standard? If so, could someone tell me why b != c?
**
** http://www.cs.caltech.edu/cgi-bin/boris/home-page?
*/


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Nate Lewis" <nlewis@mindspring.com>
Date: 1998/11/06
Raw View
Boris D. Dimitrov wrote in message <36410100.2781@caltech.edu>...
>void u(const char*       *X);
>void w(const char* const *Y)[;]
>
>void v(      char*       *Z)

>  u(Z);                             // let b = "this is an error"
>  w(Z);                             // let c = "this is an error"
>}
>
>** The DEC, SGI, and GNU compilers report that b==true and c==false.
>** Is this standard? If so, could someone tell me why b != c?


This is a close variant of a FAQ; see the message trailer.  Translated
to your syntax, consider this implementation of u:

const char c = 'a';
void u(const char** X) {
  *X = &c;
}

This function is valid.  If your call at b above were legal, you'd have
a char** pointing at a const char, and you've lost your const.  A
function with w's prototype cannot do this to its pointer and is safe.

--
Nate Lewis, MCSD
nlewis@mindspring.com





[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]