Re: NSString to char[]
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