Is it a constraint of the assignment that you must use C-style strings and arrays? Neither is appropriate for this problem, unless that requirement has been imposed.
If you are not required to use C-style strings, use std::string or QString instead.
If you are not required to use arrays, use containers (either Standard Template Library containers or Qt containers) instead.
If you must use an array of C-style strings, then calhal’s observation applies: when c_str() is applied to a temporary, the value it returns is guaranteed valid only during the statement that contains the call. (When applied to a non-temporary, it’s guaranteed valid only so long as the object to which it was applied exists and no non-const member function has been called on that object.) It’s only by chance that the code you have is working at all. Instead, you must save the QString or std::string, allocate a character buffer of the appropriate size (QString::length + 1), then copy the character data into the buffer (e.g., with strcpy). Don’t forget to delete the buffers in your clean-up code.
Much simpler and better, though, would be to use QStrings in containers.
Bookmarks