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

  • Home
  • SEARCH
  • 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 6358235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:19:52+00:00 2026-05-24T23:19:52+00:00

Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that

  • 0

Possible Duplicate:
std::vector<std::string> to char* array

I have to call a c function that accepts an array of string pointers. Example

void cFunction(char* cities[], int count)
 {
   for(int i = 0; i < count; ++i )
   {
    printf("%s\n", cities[i]);
   }
 }

Assume that function is in some third party libabry; it cannot be changed
I can declare a static array and call the function like this

char* placesA[] = {"Teakettle Junction", "Assawoman Bay", "Land O' Lakes", "Ion", "Rabbit Hask" };
cFunction(placesA, 5);

That works. But my data is dynamic i.e. the size of the array changes many times at runtime
So I tried this

std::vector<std::string> placesB(placesA, placesA + 5);
cFunction(&placesB[0], 5); // this won't work because std::string != char*[]

Tried this

std::vector<char*> placesC; 
cFunction(&placesC[0], 5);

I find placesC awkward to populate at the sametime avoid memory leaks
I am looking for a solution that is both efficient ( as little string copying as possible and preferably uses STL and or Boost )

  • 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-24T23:19:53+00:00Added an answer on May 24, 2026 at 11:19 pm

    There’s going to be some awkwardness no matter how you slice it. If the C API truly requires modifiable arrays, then that’s what you’ll need to provide — you’ll have to copy your strings into. If it doesn’t modify the strings, then you can use a std::vector of const char*, where the string data is still owned by the underlying std::string objects; you just have to be careful that the C API doesn’t hold onto references to those strings and tries to access them after the strings have been modified or deallocated.

    For example, here’s one way to do it:

    // Unary functor which calls c_str() on a std::string object
    struct StdStringCStrFunctor
    {
        const char *operator() (const std::string& str) { return str.c_str(); }
    };
    ...
    
    std::vector<std::string> places;
    ... // populate places
    
    // Convert to array of C strings
    std::vector<const char *> placesCStr(places.size());
    std::transform(places.begin(), places.end(), placesCStr.begin(), StdStringCStrFunctor());
    cFunction(const_cast<char**>(&placesCStr[0]), placesCStr.size());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ char* vs std::string Is there any advantage to using char*'s instead
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new
Possible Duplicate: How to downsize std::vector? It seems that std::vector will not release memory
Possible Duplicate: sum of elements in a std::vector I have std::vector<int> and I want
Possible Duplicate: Convert std::string to const char* or char* i've got a line from
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the
Possible Duplicate: When to use std::size_t? I have a lot of constants in my
Possible Duplicate: Maximum length of a std::basic_string<_CharT> string I would like to know how

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.