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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:18:59+00:00 2026-05-30T08:18:59+00:00

I am trying to write a function to convert a std::string to char* .

  • 0

I am trying to write a function to convert a std::string to char* .
The first one I have written was this:

char* $ (string str)
{
    char* cstr;
    const unsigned int length=str.size();
    cstr=new char[1000];
    for(int i=0;i<length;i++)
        cstr[i]=str[i];
    cstr[length]=0;
    return cstr;
}

But the problem was the memory leak: let’s suppose that I do this:

char* cstr;
string str1("hello"),str2("hello2");
cstr=$(str1);
cstr=$(str2);

There is a memory leak in this case.The first allocated string is not reachable but it’s reference is lost.
So I made the same using static:

char* $ (string str)
{
    static char cstr[1000];
    const unsigned int length=str.size();
    for(int i=0;i<length;i++)
        cstr[i]=str[i];
    cstr[length]=0;
    return cstr;
}

But the problem now is that the static char fields are accessible:

char* cstr;
string str("hello");
cstr=$(str);
$(str)[5]='!';

This is possibile, the 6th character is modified and so also the C-style string pointed by cstr is modified.

Using const:

const char* $ (string str)
{
    static char cstr[1000];
    const unsigned int length=str.size();
    for(int i=0;i<length;i++)
        cstr[i]=str[i];
    cstr[length]=0;
    return cstr;
}

The problem is that a char pointer is not compatible with a const char pointer, so I can’t do this:

string str("hello");
char* cstr;
cstr=$(str);

But I can only use a const char pointer.
What I would do is to have a function which the return value could be placed only as right operand, but not as left operand of an assignment.How could this be done?

I tried to do this:

char* toCharArray(string& str)
{
    std::unique_ptr<char>p(new char[1000]);
    char* temp=p.get();
    for(unsigned int i=0;i<str.size();i++)
    {
        *temp=str[i];
        temp++;
    }
    return p.get();
}

But the problem is still there, I don’t see the difference between this and the other solution I posted using static.Since a code like this:

char* cstr;
string str("hello");
cstr=toCharArray(str);
toCharArray(str)[0]='o';
cout << cstr;

Modifies the string (prints “oello”).
Problem still not solved.

  • 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-30T08:19:01+00:00Added an answer on May 30, 2026 at 8:19 am

    http://www.cplusplus.com/reference/string/string/c_str/

    Look at the example, specifically:

    char * cstr, *p;
    
    string str ("Please split this phrase into tokens");
    
    cstr = new char [str.size()+1];
    strcpy (cstr, str.c_str());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to convert this function: function txt(str){ var t=''; var s=unescape(str); var x=Math.round(Math.sqrt(16));
I'm trying to write a function that'll convert an integer to a string like
Im trying to write a function that will convert all charactewrs after the first
I'm trying to write a simple function to convert a std::function<> object while binding
I'm trying to write a function that formats every (string) member/variable in an object,
I'm trying to write a function that is able to determine whether a string
I'm trying to write a function to return the word string of any number
I'm trying to write simple Emacs function to convert ids between C style ones
I'm trying to write a function that takes a line as a string and
I'm trying to write a function to convert a time & date into epoch

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.