the first problem here is the temporary QByteArray... your pointer is invalid (on the next line)!
the next problem is that char** means an array of char* (in the context of main), not the address of some string (char* in C).
Your code will therefore produce doubly undetermined fun...
(sorry, but the code is just about as wrong as it can get in so few lines... )
What you want to achieve is surprisingly hard too do.
(It might be easier to pass argc/argv from main().)
QList<QByteArray> arr; // no tempory QByteArrays - the data() pointers must remain valid
std::vector<char*> v_args; // store arbitrary number of pointers in contiguous memory (like a C array)
{
arr << s.toLatin1();
v_args.push_back( arr.back().data() );
}
// vector has contig. memory, thus we can use the address of the first entry
// (yes, this code assumes that the number of arguments is not zero; which is quite safe as the program name is the first arg)
char **argv = &v_args[0];
QList<QByteArray> arr; // no tempory QByteArrays - the data() pointers must remain valid
std::vector<char*> v_args; // store arbitrary number of pointers in contiguous memory (like a C array)
foreach(const QString &s, QCoreApplication::arguments())
{
arr << s.toLatin1();
v_args.push_back( arr.back().data() );
}
// vector has contig. memory, thus we can use the address of the first entry
// (yes, this code assumes that the number of arguments is not zero; which is quite safe as the program name is the first arg)
char **argv = &v_args[0];
To copy to clipboard, switch view to plain text mode
(All this is completely untested, of course. But it should work that way.)
Lesson learned: what out whenever you want to get a char* from a QString. The danger of temporaries and dangling pointers is very real.
Bookmarks