Re: const char* to char*
Re: const char* to char*
- Subject: Re: const char* to char*
- From: Alastair Houghton <email@hidden>
- Date: Fri, 28 Nov 2003 09:23:18 +0000
On 27 Nov 2003, at 23:09, Nat! wrote:
>
const char and char are different types.
>
You can also avoid the warning like this:
>
>
>
char *s;
>
>
s = (char *) [theName cString];
>
>
>
it costs nothing and keeps the compiler quiet. I try to avoid going
>
down the "const" road :)
I think just about everybody would agree that this is poor style. The
-cString method returns a const string because you aren't supposed to
modify it (modifying it may have undesirable side-effects). Casting
away const/volatile qualifiers can make your program's behaviour
unpredictable, can introduce unpleasant (hard to find) bugs and can
even (in some cases) fool the optimiser into generating bad code. In
some cases, it can actually make your program crash (for example,
because the compiler is free to put string constants into read-only
memory).
There are only two instances in which I have found a legitimate excuse
to remove a const qualifier:
1. Calling a third-party API whose prototype is defined without a
const-qualifier, despite the fact that it is documented as not changing
its argument. VxWorks was particularly annoying for this, because even
its C library isn't const-correct.
2. Calling an API that uses a structure, some of whose members have
been defined as char * or void *; this sometimes happens because the
same structure is used for both input and output, but with different
functions, or different parameters to the same function.
There may be other good reasons for removing a const, but the above is
certainly *not* one of them.
Kind regards,
Alastair.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.