Mailinglist Archive: opensuse-packaging (183 mails)

< Previous Next >
Re: [opensuse-packaging] error: invalid conversion from 'const char*' to 'char*'
  • From: Michael Matz <matz@xxxxxxx>
  • Date: Tue, 2 Jun 2009 03:26:14 +0200 (CEST)
  • Message-id: <Pine.LNX.4.64.0906020323340.29566@xxxxxxxxxxxxx>
Hi,

On Mon, 1 Jun 2009, Martin Vidner wrote:

const char *pp = strchr(str, ';');

That is right.

Except for the case when you then modify the string through pp. (The
compiler would give you another error)
I actually had a case where I put a NUL byte at pp and then the
original character back, so I used
char *pp = (char *) strchr(str, ';');
See here for the actual code:

http://svn.opensuse.org/viewvc/yast/trunk/core/libycp/src/StaticDeclaration.cc?annotate=57367&pathrev=57367#l276

But then it's already questionable to have the incoming string be a "const
char *", as you modify it. IMO for this it's better to cast away the
constness on the argument (maybe even using a temporary) than on the
function result, as a matter of style. It seems to me more explicit of
the intent ("yes I know, this is a const string, but I also know that in
fact it's not read-only memory, so for speed purposes let's make it
'writable' for a short time").


Ciao,
Michael.
--
To unsubscribe, e-mail: opensuse-packaging+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse-packaging+help@xxxxxxxxxxxx

< Previous Next >
List Navigation