• 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: NSString to char[]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString to char[]


  • Subject: Re: NSString to char[]
  • From: pjl <email@hidden>
  • Date: Fri, 29 Jul 2005 20:58:53 -0400


On Jul 29, 2005, at 9:34 AM, Clark Cox wrote:

2005/7/24, Jerry Brace <email@hidden>:

Hey Guys,

I found a solution that works - let me know what you think:

     NSMutableString * path = [thisBundle pathForResource:@"wakein"
ofType:@""];

     unsigned int lengthOfMessage = [path length];
     char sysctlPath[lengthOfMessage + 1];
     strcpy(sysctlPath, [path UTF8String]);


Bad idea. (strlen([path UTF8String])) does not always equal ([path length]). Try this instead:

const char *sysctlPath = [path fileSystemRepresentation];

Or if you really need a char array then try:

const char *temp = [path fileSystemRepresentation];
int len = strlen(temp);
char sysctlPath [len+1];
strcpy(sysctlPath, temp);

It's only one more pointer var on the stack from what you have but it's bug-free and you can modify the string if necessary. (You're not allowed to modify the characters in a char pointer given to you from NSString hence the const). Plus it's the only correct way if you need to keep the c string around past the end of the current autorelease context or the life of the NSString.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
References: 
 >NSString to char[] (From: Jerry Brace <email@hidden>)
 >Re: NSString to char[] (From: j o a r <email@hidden>)
 >Re: NSString to char[] (From: Jerry Brace <email@hidden>)
 >Re: NSString to char[] (From: John Stiles <email@hidden>)
 >Re: NSString to char[] (From: Jerry Brace <email@hidden>)
 >Re: NSString to char[] (From: Clark Cox <email@hidden>)

  • Prev by Date: NS_HANDLER mystery
  • Next by Date: Re: Using NSFontPanel with NSControl subclasses
  • Previous by thread: Re: NSString to char[]
  • Next by thread: Re: Interpreting Documentation: Is a nil argument OK?
  • Index(es):
    • Date
    • Thread