Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:36:33+00:00 2026-05-15T15:36:33+00:00

Sorry, I’m not even sure how to ask, since I’m a complete newbie at

  • 0

Sorry, I’m not even sure how to ask, since I’m a complete newbie at C, pointers and stuff like that. There’s a function that accepts an argument: char **arg. If I write that argument like so:

char *cargs[] = {"blah", NULL};

and pass it to the function:

function(cargs);

it works. but … I have an NSArray of NSStrings and I need to make this array out of values from NSArray. I figured it should be a matter of creating a C array of the same element count as NSArray and copy the strings, converting them with cStringUsingEncoding. But I honestly have no idea how to do this, since I get confused with all those pointers and such. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:36:34+00:00Added an answer on May 15, 2026 at 3:36 pm

    Well, the rough steps can be:

    1. use count method of NSArray to know how many NSStrings are there in the NSArray.

    2. use malloc to allocate memory for cargs, something like this

      char **cargs = (char **) malloc(sizeof(char *) * count);
      

      by your example, you may need to one more room for NULL which will be at the end of cargs.

    3. use a loop and objectAtIndex: of NSArray to get out the NSStrings, like
      NSString *nsstring = [array objectAtIndex:index];

    4. use method cStringUsingEncoding: to get the c-string out, better make a copy

    5. put these c-string pointers in cargs

    6. pass cargs to your function, clean and free things needed to.

    It’s a lot of work. ‘Cause the mix of c and obj-c stuff. And a lot of manual malloc and free , messy stuff. Can’t you avoid it?

    –add sample code–

    I’m not quite sure what your real intent is. Hope this will help.

    void func(char **arg)
    {
        int i;
        for(i = 0; arg[i] != NULL; i++) {
            printf("%d=%s\n", i, arg[i]);
    }
    }
    int main(int argc, const char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        NSString *s1 = [NSString stringWithString:@"first"];
        NSString *s2 = [NSString stringWithString:@"second"];
        NSString *s3 = [NSString stringWithString:@"third"];
    
        NSArray *array = [NSArray arrayWithObjects: s1, s2, s3, nil];
        //by now, we have an NSArray of three NSStrings
    
        int count = [array count];
        char **cargs = (char **) malloc(sizeof(char *) * (count + 1));
        //cargs is a pointer to 4 pointers to char
    
        int i;
        for(i = 0; i < count; i++) {
            NSString *s = [array objectAtIndex:i];//get a NSString
            const char *cstr = [s cStringUsingEncoding:NSUTF8StringEncoding];//get cstring
            int len = strlen(cstr);//get its length
            char *cstr_copy = (char *) malloc(sizeof(char) * (len + 1));//allocate memory, + 1 for ending '\0'
            strcpy(cstr_copy, cstr);//make a copy
            cargs[i] = cstr_copy;//put the point in cargs
        }
        cargs[i] = NULL;
    
        func(cargs);//call the function to do something
    
        for(i = 0; i < count; i++) {
            free(cargs[i]);
        }
        free(cargs);
    
        [pool drain];
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry but I am not sure how to ask this question but I am
sorry if there is another question like this but i couldn't find my answer.
Sorry, not really programming question, but I am not sure where else I could
(Sorry for the not-so-good-title, I'm not sure how my problem can be solved, thus
Sorry for the odd wording, I'm not sure how to describe what I need
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Sorry, I'm not terribly experienced with Ant. I like the eclipse Export ant buildfile
SORRY:: I forgot that params[:user_id] does not exist in the controller, using current_user.id !
Sorry for this simple question, but I can't solve it... There is an example:
Sorry for the bad title - I simply do not know what to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.