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?

Eduard Malakhov
  • 1,095
  • 4
  • 13
  • 25
Josh Simani
  • 107
  • 10
  • 4
    You use `std::vector`. Which is basically `std::string`. – Hatted Rooster Jan 28 '17 at 21:41
  • 2
    Why not just use `std::string`? It gives you access to its internal character array. – Kerrek SB Jan 28 '17 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. – Josh Simani Jan 28 '17 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. – Jonas Jan 28 '17 at 21:47

2 Answers2

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());
Vishnu J
  • 501
  • 2
  • 10
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];
    
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • 1
    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 - Unslander Monica Jan 28 '17 at 21:56
  • @StoryTeller: you mean the part about vector? – Raindrop7 Jan 28 '17 at 21:58
  • Being "not it"? The part where you demonstrate with `new char[]`. It doesn't do the same thing as a local array. – StoryTeller - Unslander Monica Jan 28 '17 at 21:59
  • @StoryTeller: but a local array must have a constant size at compile-time while string not – Raindrop7 Jan 28 '17 at 22:01
  • 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. – StoryTeller - Unslander Monica Jan 28 '17 at 22:02
  • The dynamic approach is worth covering, but first show how OP should do it with a fixed-size array and point out the problems OP will run into(Inefficient sizing, potential for over flow...) that leads into the dynamic discussion which presents an opportunity to cover the problems with it as well setting you up for the `vector` approach, which you do need to rewrite. What you have is confusing because it veers away from `string` to `char` array conversions. – user4581301 Jan 28 '17 at 22:06
  • Please apply **C**apitalization rules where appropriate. In all of your answers. –  Jan 28 '17 at 22:19