2

I understand that char a[] = "Hello World"; works, but I was wondering if there was a way that you could have the array of characters initialized by a string inputed at run-time. For example:

string word;

cout << "Enter a word ";

cin >> word;

char a[] = word;

I know that clang++ does not accept this because it says, "array initializer must be an initializer list or string literal".

Is there a work around for this without using pointers?

4
  • 4
    You use std::vector<char>. Which is basically std::string. Commented Jan 28, 2017 at 21:41
  • 2
    Why not just use std::string? It gives you access to its internal character array. Commented Jan 28, 2017 at 21:42
  • I understand that you can use string as a much more simple solution, but I'm trying to learn more about character arrays and the different between them, character pointers, and strings. Commented Jan 28, 2017 at 21:44
  • 2
    The problem is that you need to allocate the memory before hand, and thus you do not know how many characters the user will enter. Commented Jan 28, 2017 at 21:47

2 Answers 2

2

Use c_str.

http://en.cppreference.com/w/cpp/string/basic_string/c_str

And something like this to copy to array:

strcpy(a, word.c_str());
Sign up to request clarification or add additional context in comments.

4 Comments

what if the length of word is greater than the array size?
I think you would need to define a MAX_SIZE int to initialize that char array to that size. If the length is too big, reprompt the user for a smaller string.
Yeah. Pretty much. Follow Raindrop7's answer.
This still requires you to know the length before hand.
2

you can create an array of characters with some fixed size like what we used in structs: char name[100]... then input string and then use strpy to copy.

char a[100];
string word;

cout << "Enter a word ";
cin >> word;

strcpy(a, word.c_str());

the code above works fine but the problem is if the user inters a text larger than 100 character then you'll UB.

in fact a stack array must have a constant size at compile time whereas a string can change the size at runtime which means you cannot do that but there's an alternative where you create a dynamic array depending on the runtime size of the string then use strcpy():

string word;
cout << "Enter a word ";
cin >> word;

char* a =  new char[word.length() + 1]; // 1 for Null character
strcpy(a, word.c_str());

 delete[] a;
  • another alternative which is my favorite is to use std::vector<char>:

    vector<char> a;
    char c;
    
    while(SomeConditionIsTrue){
        cin >> c;
        a.push_back(c);
    }
    
    for(int i(0); i < a.size(); i++)
        cout << a[i];
    

7 Comments

This answer isn't wrong, but to nitpick, it doesn't do it. Which means, it doesn't do what the OP thinks it can do. Plus, that use of a loop to fill a vector is terribly inefficient. std::vector has a constructor that accepts two iterators. You should have recommended that.
@StoryTeller: you mean the part about vector?
Being "not it"? The part where you demonstrate with new char[]. It doesn't do the same thing as a local array.
@StoryTeller: but a local array must have a constant size at compile-time while string not
Exactly. Which is why when you say "you can do it", it's wrong. Because you can't do it, and you demonstrate something completely different using dynamic allocation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.