• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: const char* to char*
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: const char* to char*


  • Subject: Re: const char* to char*
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Fri, 28 Nov 2003 00:02:38 -0800

Hello...


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 :)

Ciao
Nat!


This is probably not a good idea in general, since the whole point of declaring the return value to be const is to generate a compiler warning if you try to change the contents of memory pointed to by that address (char * is a pointer). There may be other code that has the same pointer to that block of memory (which holds the cString) and expects the string in that memory to remain unchanged. This may not happen most of the time, but there is the possibilty, and the results can be ...um... interesting.

I forget the name of the application (it was an old 7.5 era app), but a good example of this problem was an address book type application I used at one point that allowed you to duplicate an entry. So if you wanted to make a seperate entry using the same address/phone for someone's wife/husband/roommate, supposedly you could copy the original entry and then edit the copy to make the new entry without having to retype all of the other information. Unfortunately, if for example you made a copy of Joe Smith, and changed the name for the copy to Jane Doe, both entries would change the name to Jane Doe. This made the ability to duplicate a record somewhat pointless (although you could quit and reopen the application, and then you could rename one of the entries without both changing).

So if you are only reading from the memory containing the string, then there is no reason to recast the pointer, and if you leave it as const char * you will be warned if you accidently do something you shouldn't. If you are writing to the memory that contains the string, then you definitely need to make a copy of the string (not a copy of the pointer) and work with the copy of the string instead...

Louis
_______________________________________________
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.

References: 
 >Re: const char* to char* (From: Jens Bauer <email@hidden>)
 >Re: const char* to char* (From: Nat! <email@hidden>)

  • Prev by Date: Finding out the user's group
  • Next by Date: Re: status of cString (was const char* to char*)
  • Previous by thread: Re: const char* to char*
  • Next by thread: Re: const char* to char*
  • Index(es):
    • Date
    • Thread