0

(language : C++) I have this array:

 string myArray[] = {"Apple", "Ball", "Cat"};

Is it possible to store each element in the above array to a new array? Something like this.

  char word1[] = myArray[0];
  char word2[] = myArray[1];
  char word3[] = myArray[2];

I checked the above code, it would throw an error. How would I get this functionality? I cannot use two-dimensional array because I don't know the length of my word in my actual program. A file has the list of words, I would have to read it into the array and get the above string array.

2 Answers2

1

As per the example that you have posted, this is what you are looking for:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    string myArray[] = {"Apple", "Ball", "Cat"};

    char test0[myArray[0].length()];
    strcpy(test0, myArray[0].c_str());
    char test1[myArray[1].length()];
    strcpy(test1, myArray[1].c_str());
    char test2[myArray[2].length()];
    strcpy(test2, myArray[2].c_str());

    int i=0;
    for(i=0; i<(sizeof(test0)/sizeof(*test0)); i++)
        cout<<test0[i]<<" ";
    cout<<"\n";
    for(i=0; i<(sizeof(test1)/sizeof(*test1)); i++)
        cout<<test1[i]<<" ";
    cout<<"\n";
    for(i=0; i<(sizeof(test2)/sizeof(*test2)); i++)
        cout<<test2[i]<<" ";
    cout<<"\n"; 

    return 0;
}

In the above code, I have created character arrays test0[], test1[] and test2[] of length equal to the corresponding string in myArray[]. Then I used strcpy() to copy the corresponding string from myArray[] to the character array (test0[], etc). Finally, I just printed these new character arrays.

Working code here.

Note: I am assuming that you are using GCC, since it supports VLAs. If not, then you can use an array of particular length (or better yet, a vector).

Hope this is helpful.

Community
  • 1
  • 1
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
  • 1
    Standard C++ doesn't have VLAs, so `char test[str0.length()];` is not valid code. Some compilers provide it as an extension, but it's not standard. – Miles Budnek Mar 24 '17 at 22:23
  • My understanding is that declared arrays must have a compile-time constant as their capacity. Otherwise one must use the `new` operator and dynamically allocate the array. – Thomas Matthews Mar 24 '17 at 23:17
0

Old style (c++98/c++03):

#include <string>
#include <iostream>

int main()
{
    std::string myArray[] = { "Apple", "Ball", "Cat" };
    char *firstString = new char[myArray[0].length() + 1];
    char *secondString = new char[myArray[1].length() + 1];
    char *thirdString = new char[myArray[2].length() + 1];

    strcpy(firstString, myArray[0].c_str());
    strcpy(secondString, myArray[1].c_str());
    strcpy(thirdString, myArray[2].c_str());

    std::cout << "firstString = " << firstString << std::endl;
    std::cout << "secondString = " << secondString << std::endl;
    std::cout << "thirdString = " << thirdString << std::endl;

    delete firstString;
    delete secondString;
    delete thirdString;

    return 0;
}

New style (c++11/14):

#include <string>
#include <iostream>
#include <memory>

int main()
{
    std::string myArray[] = { "Apple", "Ball", "Cat" };
    std::unique_ptr<char> firstString{ new char[myArray[0].length() + 1] };
    std::unique_ptr<char> secondString{ new char[myArray[1].length() + 1]};
    std::unique_ptr<char> thirdString{ new char[myArray[2].length() + 1]};

    strcpy(firstString.get(), myArray[0].c_str());
    strcpy(secondString.get(), myArray[1].c_str());
    strcpy(thirdString.get(), myArray[2].c_str());

    std::cout << "firstString = " << firstString.get() << std::endl;
    std::cout << "secondString = " << secondString.get() << std::endl;
    std::cout << "thirdString = " << thirdString.get() << std::endl;

    return 0;
}
Tyler Lewis
  • 881
  • 6
  • 19
  • Would you please care to explain your C++11 approach? Like, why you are using a `unique_ptr<>`, the `.get()` method, etc.? –  Mar 24 '17 at 22:49
  • 1
    Yeah, no problem. Basically, std::unique_ptr is a safer replacement whenever you have something that you must create on the heap/dynamically (using the new operator). When the destructor for std::unique_ptr is called (when it falls out of scope, etc), it will automatically delete the resource held, so you never have to worry about a memory leak by forgetting to use delete. However, a std::unique_ptr is not the exact same thing as a raw pointer, and for functions that expect a raw pointer, you can use the .get() method to return the underlying raw pointer to the object you created. – Tyler Lewis Mar 24 '17 at 23:04
  • So, can I use `unique_ptr<>` majority of the times when I would require the `new` operator? Also, could you please point to some resources wherein I would learn about how to *apply* my C++11 knowledge? I know C++, but not C++11, since I didn't practice it. I would appreciate some resources to practice. –  Mar 24 '17 at 23:27
  • 1
    Yes! That's exactly right. Idiomatic C++11 means you try to use new and delete as scarcely as possible. C++11 is really an entirely new language, it has so many good features. I learned all of my C++11/14 knowledge from Scott Meyer's "Effective Modern C++". It is of course excellent, but expect to read it a few times to grasp the concepts. I found it extremely terse, and so much high level information is packed into such a small book that you really have to take it slow. In regards to truly using it, rewrite old projects using as much c++11 as you can, and apply the concepts from that book. – Tyler Lewis Mar 24 '17 at 23:39