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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:33:10+00:00 2026-05-18T00:33:10+00:00

In a recent question , I learned that there are situations where you just

  • 0

In a recent question, I learned that there are situations where you just gotta pass a char* instead of a std::string. I really like string, and for situations where I just need to pass an immutable string, it works fine to use .c_str(). The way I see it, it’s a good idea to take advantage of the string class for its ease of manipulation. However, for functions that require an input, I end up doing something like this:

std::string str;
char* cstr = new char[500]; // I figure dynamic allocation is a good idea just
getstr(cstr);               // in case I want the user to input the limit or
str = cstr;                 // something. Not sure if it matters.
delete[] cstr;
printw(str.c_str());

Obviously, this isn’t so, uh, straightforward. Now, I’m pretty new to C++ so I can’t really see the forest for the trees. In a situation like this, where every input is going to have to get converted to a C string and back to take advantage of string‘s helpful methods, is it just a better idea to man up and get used to C-style string manipulation? Is this kind of constant back-and-forth conversion too stupid to deal with?

  • 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-18T00:33:10+00:00Added an answer on May 18, 2026 at 12:33 am

    In the example you give, you can generally read a line into a std::string using the std::getline function: http://www.cplusplus.com/reference/string/getline/

    Of course this doesn’t do everything that a curses library does. If you need a non-const char* so that some C function can read into it, you can use a vector<char>. You can create a vector<char> from a string, and vice-versa:

    std::string       a("hello, world");
    std::vector<char> b(a.begin(), a.end());
    
    // if we want a string consisting of every byte in the vector
    std::string       c(b.begin(), b.end());
    
    // if we want a string only up to a NUL terminator in the vector
    b.push_back(0);
    std::string       d(&b[0]);
    

    So your example becomes:

    std::vector<char> cstr(500);
    getnstr(&cstr[0], 500);
    printw(&cstr[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a recent question of mine I learned that if there are more than
A recent question about string literals in .NET caught my eye. I know that
A recent question came up about using String.Format(). Part of my answer included a
As just stated in a recent question and answer , you can't inherit from
There is another recent Project Euler question but I think this is a bit
A recent question about StyleCop alerted me to the use of tools to enforce
This recent question about sorting randomly using C# got me thinking about the way
Following on from my recent question on Large, Complex Objects as a Web Service
In a recent question on stubbing, many answers suggested C# interfaces or delegates for
Following on from my recent question regarding parsing XML files in Java I have

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.