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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:40:29+00:00 2026-05-24T12:40:29+00:00

I have a std::vector<std::string> that I need to use for a C function’s argument

  • 0

I have a std::vector<std::string> that I need to use for a C function’s argument that reads char* foo. I have seen how to convert a std::string to char*. As a newcomer to C++, I’m trying to piece together how to perform this conversion on each element of the vector and produce the char* array.

I’ve seen several closely related SO questions, but most appear to illustrate ways to go the other direction and create std::vector<std::string>.

  • 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-24T12:40:30+00:00Added an answer on May 24, 2026 at 12:40 pm

    You can use std::transform as:

    std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);  
    

    Which requires you to implement convert() as:

    char *convert(const std::string & s)
    {
       char *pc = new char[s.size()+1];
       std::strcpy(pc, s.c_str());
       return pc; 
    }
    

    Test code:

    int main() {
           std::vector<std::string>  vs;
           vs.push_back("std::string");
           vs.push_back("std::vector<std::string>");
           vs.push_back("char*");
           vs.push_back("std::vector<char*>");
           std::vector<char*>  vc;
    
           std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);   
    
           for ( size_t i = 0 ; i < vc.size() ; i++ )
                std::cout << vc[i] << std::endl;
    
           for ( size_t i = 0 ; i < vc.size() ; i++ )
                delete [] vc[i];
    }
    

    Output:

    std::string
    std::vector<std::string>
    char*
    std::vector<char*>
    

    Online demo : http://ideone.com/U6QZ5

    You can use &vc[0] wherever you need char**.

    Note that since we’re using new to allocate memory for each std::string (in convert function), we’ve to deallocate the memory at the end. This gives you flexibility to change the vector vs; you can push_back more strings to it, delete the existing one from vs, and vc (i.e vector<char*> will still be valid!

    But if you don’t want this flexibility, then you can use this convert function:

    const char *convert(const std::string & s)
    {
       return s.c_str();
    }
    

    And you’ve to change std::vector<char*> to std::vector<const char*>.

    Now after the transformation, if you change vs by inserting new strings, or by deleting the old ones from it, then all the char* in vc might become invalid. That is one important point. Another important point is that, you don’t need to use delete vc[i] in your code anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string std::string s = Stack Overflow; That I need to copy
I have a std::string that contains comma separated values, i need to store those
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have a std::vector. I want to create iterators representing a slice of that
Say, I have a std::vector<SomeClass *> v; in my code and I need to
I have a static std::vector in a class. When I use Microsoft's memory leak
I have a class Message that has a std::string as a data member, defined
It seems that I can sort a std::vector<std::pair<int, std::string>> , and it will sort
i have a specific problem with pointers and references, with std::vector and std::string. Some

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.