pointer to array of QString.
-
wrote on 22 Aug 2016, 15:56 last edited by
Hi everyone,
I have a question for you, doctor in programming world.
I have two files .cpp. In one of these I have an array of QString and I want to get it from the other file.
The name of QString array is history_array[30]So in the first file I implement this function:
QString history_array[30]; ... void Data_mng::get_userHistory_array (QString *p) { *p=history_array; }
while in the second file I implement these two code lines to get the array.
Data_mng dat; QString *test[30]; dat.get_userHistory_array(&test);
Who can tell me where I'm doing wrong? Who can tell me how can I solve it?
I think it's not a simple question...the array of QString is not easy to manage....Thank you folks!
-
wrote on 22 Aug 2016, 16:56 last edited by
*p
is of typeQString
,history_array
is of typeQString[]
you cannot=
the two&test
is of typeQString**[]
so does not match the function signaturevoid Data_mng::get_userHistory_array (QString *& p) { p=history_array; }
Data_mng dat; QString *test; dat.get_userHistory_array(test);
P.S.
This design looks really bad but I don't think it's the point here -
*p
is of typeQString
,history_array
is of typeQString[]
you cannot=
the two&test
is of typeQString**[]
so does not match the function signaturevoid Data_mng::get_userHistory_array (QString *& p) { p=history_array; }
Data_mng dat; QString *test; dat.get_userHistory_array(test);
P.S.
This design looks really bad but I don't think it's the point herewrote on 22 Aug 2016, 18:11 last edited byThank you Robin for you help.
It works, but I have the access only to the first element of the array..Do you know how can I get the entire array?
Thanks, again
-
Thank you Robin for you help.
It works, but I have the access only to the first element of the array..Do you know how can I get the entire array?
Thanks, again
@FranciscoSantiago
Hi
No matter what you do the syntax will be ugly and the size (30) will fast get lost.
Is there any reason you cannot use a QStringList ?also, if you keep the list inside a class, why give it away to outside code?
why not do
QString Data_mng::get_userHistory_str (int index) {
// check index
return history_array[index];
} -
@FranciscoSantiago
Hi
No matter what you do the syntax will be ugly and the size (30) will fast get lost.
Is there any reason you cannot use a QStringList ?also, if you keep the list inside a class, why give it away to outside code?
why not do
QString Data_mng::get_userHistory_str (int index) {
// check index
return history_array[index];
}wrote on 22 Aug 2016, 19:54 last edited by@mrjj
I didn't know what QStringList was..Thank you.. I go to discover it.. ;)
-
@mrjj
I didn't know what QStringList was..Thank you.. I go to discover it.. ;)
@FranciscoSantiago
its a better way to have list of QStringshttp://doc.qt.io/qt-5/qstringlist.html#details
and you can just do
QStringList & Data_mng::GetThelist() {
return history_list;
} -
Thank you Robin for you help.
It works, but I have the access only to the first element of the array..Do you know how can I get the entire array?
Thanks, again
wrote on 23 Aug 2016, 04:49 last edited by RajeeshRaveendranincrement the pointer by 1 to get the next QString object.
eg:-
QString strings[30]; /// Array of 30 strings
QString* pStrings = strings; /// Pointer to string array.QString firstString = *(pStrings + 0);
QString secString = *(pStrings + 1);
QString thirdString = *(pStrings + 2); /// You can also loop it. But beware that you are not accessing beyond 30th item.
...But anyway it is better to use QStringList instead(it is easy and you can save lot of efforts as above code)
Regards,
Rajeesh Raveendran
1/7