• 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: NSArray to argv-style string vector. How ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray to argv-style string vector. How ?


  • Subject: Re: NSArray to argv-style string vector. How ?
  • From: Rolf <email@hidden>
  • Date: Mon, 23 Jun 2003 00:34:13 +0200

Hi Nick Zitzmann,

Thanks for your help. Your method works perfectly :-)
I just had to increase the malloc() length by 1 (for the terminating char), and add a NULL element to array as follows:

NSArray *array; // or whatever you called your array object
const unsigned arrayCount = [array count];
char *charArray[arrayCount + 1];
int charArrayCount;

for (charArrayCount = 0; charArrayCount < arrayCount; charArrayCount++)
{
NSString *theString = (NSString *)[array objectAtIndex:charArrayCount];
unsigned int stringLength = [theString length];
charArray[charArrayCount] = malloc((stringLength + 1) * sizeof(char));
snprintf(charArray[charArrayCount], stringLength + 1, "%s", [theString cString]);
}
charArray[charArrayCount] = NULL;


/Rolf


22.06.2003 23:45:02, skrev Nick Zitzmann <email@hidden>:

>
>On Sunday, June 22, 2003, at 11:28 AM, Rolf wrote:
>
>> I need to call AuthoriztionExecuteWithPriveliges(). The fourth
>> parameter is of type argv-style string vector (char * const
>> *arguments). Does anybody know how to convert an NSArray of
>> NSStrings's to fit this fourth parameter ? I'm sure there's an
>> embarrassingly simple way to do this ..
>
>You could do it dynamically, like this: (code written in Mail,
>untested, etc.)
>
>NSArray *array; // or whatever you called your array object
>const unsigned arrayCount = [array count];
>char *charArray[arrayCount];
>int charArrayCount;
>
>for (charArrayCount = 0 ; charArrayCount < arrayCount ;
>charArrayCount++)
>{
> NSString *theString = [array objectAtIndex:charArrayCount];
> unsigned int stringLength = [theString length];
>
> charArray[charArrayCount] = malloc(stringLength * sizeof(char));
> snprintf(charArray[charArrayCount], stringLength, "%s", [theString
>cString]; // I'm assuming you're not using any 2-byte characters here
>}
>
>// do your Security calls here
>
>// this will de-allocate the memory we allocated above
>while (charArrayCount >= 0)
>{
> free(charArray[charArrayCount]);
> charArrayCount--;
>}
>
>Nick Zitzmann
>AIM/iChat: dragonsdontsleep
>Check out my software page: http://dreamless.home.attbi.com/
>
>"Great spirits have always encountered violent opposition from mediocre
>minds." - Albert Einstein
_______________________________________________
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: NSArray to argv-style string vector. How ? (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: Use libcddb in my Cocoa App
  • Next by Date: Re: Use libcddb in my Cocoa App
  • Previous by thread: Re: NSArray to argv-style string vector. How ?
  • Next by thread: Set exit code without using exit() ?
  • Index(es):
    • Date
    • Thread