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 8754203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:34:23+00:00 2026-06-13T13:34:23+00:00

I need to convert an NSarray filled with NSStrings and return this c array

  • 0

I need to convert an NSarray filled with NSStrings and return this c array to the function.

-(char**) getArray{
        int count = [a_array count];
        char** array = calloc(count, sizeof(char*));

        for(int i = 0; i < count; i++)
        {
             array[i] = [[a_array objectAtIndex:i] UTF8String];
        }
        return array;     
}

I have this code, but when should i free the memory if I’m returning stuff?

  • 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-06-13T13:34:24+00:00Added an answer on June 13, 2026 at 1:34 pm

    You need to allocate memory for each string in the array as well. strdup() would work for this. You also need to add a NULL to the end of the array, so you know where it ends:

    - (char**)getArray
    {
        unsigned count = [a_array count];
        char **array = (char **)malloc((count + 1) * sizeof(char*));
    
        for (unsigned i = 0; i < count; i++)
        {
             array[i] = strdup([[a_array objectAtIndex:i] UTF8String]);
        }
        array[count] = NULL;
        return array;     
    }
    

    To free the array, you can use:

    - (void)freeArray:(char **)array
    {
        if (array != NULL)
        {
            for (unsigned index = 0; array[index] != NULL; index++)
            {
                free(array[index]);
            }
            free(array);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need convert this array data = [ #SHO 0x56, 0x0d, #CMD 0x1, 0x00,
I Need Convert This Code To .net (C# or VB.net) Please Help Me For
Can we convert an NSArray to a C array? If not what alternatives, are
I need to convert a StringBuffer Array to String Array for sorting. Is there
I have NSArray that contains NSDictionaries. I need to convert it to CFMutableArray that
I need to send an NSArray to the server in the JSON array format.
I need convert a String to a decimal in C#, but this string have
I am in a situation that needs be solve with this way; need convert
I have this minutes number 2400 . I need convert 2400 minutes to exact
I am using the NSString in my program i need to convert this into

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.