Thread: Initialize char array with string

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    8

    Initialize char array with string

    Can I do something like this in C++:

    Code:
    string line;
    char sentence[];
    
    cout << "Please enter a string: ";
    cin >> line;
    
    sentence = line;

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Can I do something like this in C++
    The last part is not correct but you can have something like

    Code:
    string line;
    char *sentence;
    
     
    cout << "Please enter a string: ";
    cin >> line;
     
    sentence = line.c_str();

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    @Aslaville
    Your sentence pointer should be const in your example since that's what c_str() returns (and you're not allowed to modify the data through that pointer). Maybe the OP wants a copy:
    Code:
    char *sentence = new char[line.size() + 1];
    std::strcpy(sentence, line.c_str());        // include <cstring> header
    Remember to delete [] sentence at some point.
    Last edited by algorism; 04-15-2016 at 03:42 PM.

  4. #4
    Registered User
    Join Date
    Apr 2016
    Posts
    8
    I was doing that and get the following:

    cannot convert from 'const char *' to 'char *'

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you really want a C-string copy of a string for some reason, I prefer this:
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string line = "Some string from somewhere\n";
    
        char *sentence = new char [line.length() + 1];
    
        size_t sentence_length = line.copy(sentence, line.length() - 1);
    
        sentence[sentence_length] = '\0';
    
        cout << "The sentence is " << sentence_length << " characters long.\n";
        cout << "sentence=\"" << sentence << "\"\n";   
        cout << "Make whatever copies you want! Here\'s a substring!\n";
    
        sentence_length = line.copy(sentence, 6, 5);
        sentence[sentence_length] = '\0';
        
        cout << "The sentence is " << sentence_length << " characters long.\n";
        cout << "sentence=\"" << sentence << "\"\n";
    
        delete[] sentence;
        sentence = 0;
    }
    Run the code, see for yourself.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Why is no one in this thread using smart pointers?!?!?!

    OP, for reference, a smart pointer is simply a type where you construct it with a pointer and when the smart pointer goes out of scope, the memory is automatically freed.

    Basically, it's like:
    Code:
    std::unique_ptr<char[]> sentence{new char[length_of_string]};

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why is no one in this thread using smart pointers?!?!?!
    Them having nothing to do with the question might be a reason.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    Them having nothing to do with the question might be a reason.
    They became relevant once algorism introduced new[] and mentioned delete[].

    Anyway, no point using new[] and delete[] here, with or without a smart pointer. Aslaville's suggestion would be applicable if the null terminated string will not be modified, but since it apparently will be, it would suffice to null terminate a std::string:
    Code:
    cout << "Please enter a string: ";
    string line;
    cin >> line;
    
    line.push_back('\0');
    char* sentence = &line[0];
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bus Error Appending Char to String (char array)
    By seal308 in forum C Programming
    Replies: 8
    Last Post: 02-02-2016, 12:12 PM
  2. Replies: 8
    Last Post: 01-26-2012, 02:40 PM
  3. Initialize 2 dimensional char array
    By skiabox in forum C Programming
    Replies: 4
    Last Post: 11-01-2010, 06:59 AM
  4. help with simple initialize 2-d char array
    By LightYear in forum C Programming
    Replies: 12
    Last Post: 04-26-2010, 09:59 PM
  5. initialize 2 dimensional char array
    By meka in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 02:13 PM