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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:25:54+00:00 2026-06-02T15:25:54+00:00

Is it possible to do this std::string str(const char* s) { return std::string(s); }

  • 0

Is it possible to do this

std::string str(const char* s)
{  
     return std::string(s);
} 

int main() {  
    char* strz = (char*)str("asd").c_str();  
}  

Instead of:

int main(){  
    std::string temp = str("asd");  
    char* strz = (char*)temp.c_str();  
}

I know it should be const char* strz but I need it only within block of code(and without new/delete). After returning string from method it look for reference(if cant find it, deletes string) and then calls c_str(). I have a lot of char’s(independent from me) and I could use second solution but it takes too much code.

  • 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-02T15:25:57+00:00Added an answer on June 2, 2026 at 3:25 pm

    If you use your second option –

    std::string temp = str("asd");
    char* strz = (char*)temp.c_str();
    

    You’re possibly running into undefined behavior. It’s illegal to modify the contents of strz. You’ll have to use strcpy to get a mutable array of char‘s. Your first isn’t any better either, moreover, it’s redundant, since you can directly use string‘s constructor.

    Anyway, to get a char* from a string you can do:

    char* get(const std::string& str)
    {
       char* ret = new char[str.length() + 1]();
       strcpy(ret,str.c_str());
       return ret;
    }
    

    You’ll have to delete[] the memory yourself when you’re done with it.

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

Sidebar

Related Questions

class logger { .... }; logger& operator<<(logger& log, const std::string& str) { cout <<
Let's look at a simple example: struct some_struct { std::string str; int a, b,
Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Referring to this web site http://www.cplusplus.com/reference/std/utility/make_pair/ The std::make_pair has this signature (and possible implementation):
i just wrote a function: void doSomeStuffWithTheString(const std::string& value) { ... std::string v =
Here is my code: fs::path datadir = ...; std::string dataDirOption((boost::format(--datadir=%1%) % datadir).str()); For datadir==c:/db
I am trying to write a function to convert a std::string to char* .
Possible Duplicate: Printing a string to a temporary stream object in C++ std::ostringstream printing
class Foo { double f1( int x, std::string s1 ); double f2( int x,

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.