Skip navigation.
 
mlRe: Cocoa string to carbon compatible C string
FROM : Jens Alfke
DATE : Sun May 11 22:38:58 2008

On 11 May '08, at 10:36 AM, Steve Cronin wrote:

>     const char *  cPartNumber = [partNumber 
> cStringUsingEncoding:NSUTF8StringEncoding];


That looks correct, assuming the Carbon function does take a C string 
in UTF-8 encoding, but you can make it a bit more compact:

   const char *  cPartNumber = [partNumber UTF8String];

>                     (char *) cPartNumber,


It would be a good idea to check whether that function takes a char* 
(instead of const char*) because it actually writes back into the 
string, or because someone just forgot to put a "const" in its 
declaration. Most likely it's the latter, but in the former case 
there's a chance it could blow up the heap if it decides to overwrite 
the C string generated by the NSString.

—Jens

Related mailsAuthorDate
mlCocoa string to carbon compatible C string Steve Cronin May 11, 19:36
mlRe: Cocoa string to carbon compatible C string Nick Zitzmann May 11, 19:59
mlRe: Cocoa string to carbon compatible C string Jens Alfke May 11, 22:38
mlRe: Cocoa string to carbon compatible C string Steve Cronin May 12, 00:11
mlRe: Cocoa string to carbon compatible C string Nick Zitzmann May 12, 00:14
mlRe: Cocoa string to carbon compatible C string Scott Ribe May 12, 19:41