delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/08/01/20:30:17

From: hhkraemer AT web DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: char*[] -> const char** conversion
Date: Thu, 02 Aug 2001 00:22:01 GMT
Lines: 59
Message-ID: <3b68971b.2701444@news.cis.dfn.de>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1010730085217 DOT 11994C-100000 AT is> <90EFCD60BNiklasPsonnospamhotm AT 130 DOT 235 DOT 20 DOT 4>
NNTP-Posting-Host: pd9002135.dip.t-dialin.net (217.0.33.53)
X-Trace: fu-berlin.de 996711706 3722028 217.0.33.53 (16 [27606])
X-Newsreader: Forte Free Agent 1.21/32.243
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On 31 Jul 2001 17:16:31 GMT, Niklas_Pson AT nosmam DOT hotmail DOT com (Niklas
Pettersson) wrote:

> djgpp AT delorie DOT com (Eli Zaretskii) wrote in
> >I think this is a standard issue with C++: const char and char are not 
> >the same, and you aren't allowed to mix them.  Even C compilers print 
> >warnings about that nowadays.
> >
> 
> Hm..Eli is right about that const char and char are different types in C++ 
> but I think that the code given should work.. Const char * in the function 
> prototype just says that the function cannot change the parameter, but a 
> non const char can still be passed as an actual parameter. But the otherway 
> around is an error of course.. 

Please note that is not an assignment of a

	 char *

to a

	 char const *

which is always safe and converted implicitly but an assignemnt of a

	char **

to a

	char const **

which is _not_ safe and therefore not done implicitly. Take this
standard example:

#include <stdio.h>

int main()
{

        char      nc[]  = "NONCONST"; 
        char const *cp  = "CONST";
        char* p = nc; // OK
        char **pp = &p ; // OK

        char const **cpp = pp; // assume that this assignment would
	                       // be OK implicitly

        *cpp = cp; // OK because *cpp is a char const*

        **pp ='X'; // oops, This is changing the first character
                      of "CONST". The program might crash...

        puts(*pp); // ???
        return 0;
 }

Regards
Horst
 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019