pointer to array of QString.



  • 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!



  • *p is of type QString, history_array is of type QString[] you cannot = the two

    &test is of type QString**[] so does not match the function signature

    void 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



  • @VRonin

    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


  • Qt Champions 2016

    @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];
    }



  • @mrjj
    I didn't know what QStringList was..

    Thank you.. I go to discover it.. ;)


  • Qt Champions 2016

    @FranciscoSantiago
    its a better way to have list of QStrings

    http://doc.qt.io/qt-5/qstringlist.html#details

    and you can just do

    QStringList & Data_mng::GetThelist() {
    return history_list;
    }



  • @FranciscoSantiago

    increment 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.