Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to pass and simply iterate...C/C++
Forum Updated to NodeBB v4.3 + New Features

How to pass and simply iterate...C/C++

Scheduled Pinned Locked Moved Unsolved C++ Gurus
17 Posts 3 Posters 2.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Anonymous_Banned275

    @Chris-Kawa Ok, as I thought - I need to pass the array index as a separate parameter so the iteration will go thru correct array. .
    The iteration is limited to size of the array, so it is not in danger to "run over".
    .

    S Offline
    S Offline
    SimonSchroeder
    wrote on last edited by
    #8

    @AnneRanch If you know that ProcessCommand() is just called with a single QStringList, it is better to have just a single QStringList as parameter and provide the correct QStringList from the array when calling this function. Then you don't have to pass the index to the function (ProcessCommand does not have any stakes in knowing it is processing an entry from an array).

    This would look like this:

    // function declaration:
    QString BT_Utility_Library::ProcessCommand(char *command, const QStringList &verify);
    // calling the function
    BT_Utility_Library btul;
    char *command = ...;
    QStringList verify[MAX_LIST] = ...;
    ...
    btul.ProcessCommand(command, verify[0]);
    

    Note that I again defined the QStringList parameter as reference. The difference to Java is that in Java everything is already a reference. This is why you don't have to specify it in Java. Almost always you are correct in using a const reference when passing objects. If you don't do this you will have copies all over the place which eventually slows down your application significantly.

    1 Reply Last reply
    1
    • A Offline
      A Offline
      Anonymous_Banned275
      wrote on last edited by
      #9

      One more question .
      I am trying to have two dimensional array and cannot get the
      for ( ....) to retrieve the members - it retrieves only the first sub array.

      // verify two dimensional list
      for (auto &verify_list : *verify[index]) {
      text = "verify list ";
      text += verify_list;
      qDebug() << text;
      }

      then I get this error

      /mnt/RAID_124/PROJECTS_MAR6_BAD_RECOVERY/CCC_SOURCE/BT_Utility_Library/bt_utility_library.cpp:95: error: indirection requires pointer operand ('QStringList' invalid)
      bt_utility_library.cpp:95:40: error: indirection requires pointer operand ('QStringList' invalid)
      for (auto &verify_list : *verify[index]) {
      ^~~~~~~~~~~~~~

      I do not know know how to implement "pointer operand" .
      Help would be appreciated.

      Chris KawaC 1 Reply Last reply
      0
      • A Anonymous_Banned275

        One more question .
        I am trying to have two dimensional array and cannot get the
        for ( ....) to retrieve the members - it retrieves only the first sub array.

        // verify two dimensional list
        for (auto &verify_list : *verify[index]) {
        text = "verify list ";
        text += verify_list;
        qDebug() << text;
        }

        then I get this error

        /mnt/RAID_124/PROJECTS_MAR6_BAD_RECOVERY/CCC_SOURCE/BT_Utility_Library/bt_utility_library.cpp:95: error: indirection requires pointer operand ('QStringList' invalid)
        bt_utility_library.cpp:95:40: error: indirection requires pointer operand ('QStringList' invalid)
        for (auto &verify_list : *verify[index]) {
        ^~~~~~~~~~~~~~

        I do not know know how to implement "pointer operand" .
        Help would be appreciated.

        Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #10

        @AnneRanch

        indirection requires pointer operand

        This means * (indirection operator) can be used on pointers and what you have here is QStringList, so not a pointer. You don't need * here.

        it retrieves only the first sub array

        See my previous response where I posted how to iterate over both dimensions.

        for (auto &verify_list : *verify[index]) {
        text = "verify list ";
        text += verify_list;
        qDebug() << text;
        }

        Note that you're overwriting text in each loop iteration. If you want to join the strings from the list and display it as one string you would do it like this:

        QString joined = verify[index].join(' ');
        qDebug() << joined;
        

        If you want to display each string separately do it like this:

        for (const auto& str : verify[index])
        {
           text = "verify list " + str;
           qDebug() << text;
        }
        
        1 Reply Last reply
        1
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #11

          @Chris-Kawa said in How to pass and simply iterate...C/C++:

          for (const auto& verify_list : verify[0])

          Here is my problem
          'debug (verify) clearly reads correct array - index[1] , but the iterator (verify_list)
          "prints" the wrong one - index [0] .

          What am I doing wrong?

          ae98d762-486e-45cd-b2a3-74fe18a9f4a8-image.png

          Chris KawaC 1 Reply Last reply
          0
          • A Anonymous_Banned275

            @Chris-Kawa said in How to pass and simply iterate...C/C++:

            for (const auto& verify_list : verify[0])

            Here is my problem
            'debug (verify) clearly reads correct array - index[1] , but the iterator (verify_list)
            "prints" the wrong one - index [0] .

            What am I doing wrong?

            ae98d762-486e-45cd-b2a3-74fe18a9f4a8-image.png

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #12

            @AnneRanch I see nothing wrong. From what I can tell from your picture the content of your structure is:
            verify[0] is QStringList { "Discovery started", "SPP_CA" }
            verify[1] is QStringList { "Controller", "Agent registered" }

            The debugger shows index is 1 so the output shows

            verify list Controller
            verify list Agent registered
            

            as expected. Are you maybe confused about what *verify is in the debug window? Since it's an array *verify is the same as verify[0], so it's all as expected.

            A 1 Reply Last reply
            2
            • Chris KawaC Chris Kawa

              @AnneRanch I see nothing wrong. From what I can tell from your picture the content of your structure is:
              verify[0] is QStringList { "Discovery started", "SPP_CA" }
              verify[1] is QStringList { "Controller", "Agent registered" }

              The debugger shows index is 1 so the output shows

              verify list Controller
              verify list Agent registered
              

              as expected. Are you maybe confused about what *verify is in the debug window? Since it's an array *verify is the same as verify[0], so it's all as expected.

              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by Anonymous_Banned275
              #13

              @Chris-Kawa

              Here is how the array looks and under index 1 the iterator should return
              "Discovery started ' and "SPP_CA" - it does not. iT returns index 0.
              .

              8b72b400-84e8-47f3-88bc-d41a279dc03b-image.png

              Chris KawaC 1 Reply Last reply
              0
              • A Anonymous_Banned275

                @Chris-Kawa

                Here is how the array looks and under index 1 the iterator should return
                "Discovery started ' and "SPP_CA" - it does not. iT returns index 0.
                .

                8b72b400-84e8-47f3-88bc-d41a279dc03b-image.png

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #14

                @AnneRanch But that's not the same thing as what you have on the previous screenshot, so either the contents of the array are changing each time or I don't know what you're posting, because those two posts don't match.
                And what iterator are you talking about? There are no iterators in that for loop you posted previously.

                You do a lot of copy pasting. Maybe you have two different variables with the same name in different places or something?

                A 1 Reply Last reply
                1
                • Chris KawaC Chris Kawa

                  @AnneRanch But that's not the same thing as what you have on the previous screenshot, so either the contents of the array are changing each time or I don't know what you're posting, because those two posts don't match.
                  And what iterator are you talking about? There are no iterators in that for loop you posted previously.

                  You do a lot of copy pasting. Maybe you have two different variables with the same name in different places or something?

                  A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on last edited by Chris Kawa
                  #15

                  @Chris-Kawa I am making some very basic mistake defining the array, but I just cannot see it.

                  I have added plain for(...) loop and forced the verify index to 0 and 1

                  The plain loop results does not make sense - the results are "reversed " .

                  The key . in my opinion, is in my definition of the two dimensional array- somewhere I am making a mistake ...

                              //#ifdef BYPASS
                               // verify two dimensional list'
                               for (const auto& verify_list : verify[index])
                               {
                  
                               //for (const auto  &verify_list : *verify[index]) {
                                   text = "verify list  ";
                                   text += verify_list;
                                   qDebug()  << text;
                               }
                               int size = 2;
                              for(int Loop_index =0; Loop_index != size ; Loop_index++)
                              {
                                  text = verify[index][Loop_index];
                                  qDebug() << text;
                              }
                  
                              text = "Process index...zero ";
                              text += QString::number(index); // _FUNC_INFO;
                              qDebug() << text;
                  
                  
                              for(int Loop_index =0; Loop_index != size ; Loop_index++)
                              {
                                  text = verify[0][Loop_index];
                                  qDebug() << text;
                              }
                  
                              text = "Process index...one  ";
                              text += QString::number(index); // _FUNC_INFO;
                              qDebug() << text;
                  
                              for(int Loop_index =0; Loop_index != size ; Loop_index++)
                              {
                                  text = verify[1][Loop_index];
                                  qDebug() << text;
                              }
                  
                  
                               return result;
                  
                  "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
                  "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
                  "Process index...1"
                  "verify list  Controller"
                  "verify list  Agent registered"
                  "Controller"
                  "Agent registered"
                  "Process index...zero 1"
                  "Discovery started"
                  "SPP_CA"
                  "Process index...one  1"
                  "Controller"
                  "Agent registered"
                  "void MainWindow_Bluetooth::Process_Menu_Index(int)"
                  
                   #define MAX_ARRAY_VERIFY 16
                      QStringList verify[MAX_ARRAY_VERIFY] = {
                          {    // verify Agent registered
                               "Controller",
                               "Agent registered"
                          },
                          {    // verify Agent registered
                               // temporqry MN
                               "Discovery started",
                               "SPP_CA"
                          },
                          {    // verify Agent registered
                               "Controller",
                               "Agent registered"
                          }
                      };
                  
                  Chris KawaC 1 Reply Last reply
                  0
                  • A Anonymous_Banned275

                    @Chris-Kawa I am making some very basic mistake defining the array, but I just cannot see it.

                    I have added plain for(...) loop and forced the verify index to 0 and 1

                    The plain loop results does not make sense - the results are "reversed " .

                    The key . in my opinion, is in my definition of the two dimensional array- somewhere I am making a mistake ...

                                //#ifdef BYPASS
                                 // verify two dimensional list'
                                 for (const auto& verify_list : verify[index])
                                 {
                    
                                 //for (const auto  &verify_list : *verify[index]) {
                                     text = "verify list  ";
                                     text += verify_list;
                                     qDebug()  << text;
                                 }
                                 int size = 2;
                                for(int Loop_index =0; Loop_index != size ; Loop_index++)
                                {
                                    text = verify[index][Loop_index];
                                    qDebug() << text;
                                }
                    
                                text = "Process index...zero ";
                                text += QString::number(index); // _FUNC_INFO;
                                qDebug() << text;
                    
                    
                                for(int Loop_index =0; Loop_index != size ; Loop_index++)
                                {
                                    text = verify[0][Loop_index];
                                    qDebug() << text;
                                }
                    
                                text = "Process index...one  ";
                                text += QString::number(index); // _FUNC_INFO;
                                qDebug() << text;
                    
                                for(int Loop_index =0; Loop_index != size ; Loop_index++)
                                {
                                    text = verify[1][Loop_index];
                                    qDebug() << text;
                                }
                    
                    
                                 return result;
                    
                    "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
                    "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
                    "Process index...1"
                    "verify list  Controller"
                    "verify list  Agent registered"
                    "Controller"
                    "Agent registered"
                    "Process index...zero 1"
                    "Discovery started"
                    "SPP_CA"
                    "Process index...one  1"
                    "Controller"
                    "Agent registered"
                    "void MainWindow_Bluetooth::Process_Menu_Index(int)"
                    
                     #define MAX_ARRAY_VERIFY 16
                        QStringList verify[MAX_ARRAY_VERIFY] = {
                            {    // verify Agent registered
                                 "Controller",
                                 "Agent registered"
                            },
                            {    // verify Agent registered
                                 // temporqry MN
                                 "Discovery started",
                                 "SPP_CA"
                            },
                            {    // verify Agent registered
                                 "Controller",
                                 "Agent registered"
                            }
                        };
                    
                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by Chris Kawa
                    #16

                    @AnneRanch That output does not match the code. You must be iterating over a different array or verify in the loops is not the same variable as the one in the array declaration.

                    Where do you get the verify variable? Is it still from the function argument
                    ProcessCommand ( char* command, QStringList verify[] ) ?
                    What do you pass to that function when you call it? Maybe you passed a pointer to second element by mistake.

                    Put this just before the first loop:

                    qDebug() << verify[0] << verify[1] << verify[2];
                    

                    to make sure the contents of the array are what you think they are, because it looks like the array you are actually iterating over doesn't have that first list in it.

                    A 1 Reply Last reply
                    1
                    • Chris KawaC Chris Kawa

                      @AnneRanch That output does not match the code. You must be iterating over a different array or verify in the loops is not the same variable as the one in the array declaration.

                      Where do you get the verify variable? Is it still from the function argument
                      ProcessCommand ( char* command, QStringList verify[] ) ?
                      What do you pass to that function when you call it? Maybe you passed a pointer to second element by mistake.

                      Put this just before the first loop:

                      qDebug() << verify[0] << verify[1] << verify[2];
                      

                      to make sure the contents of the array are what you think they are, because it looks like the array you are actually iterating over doesn't have that first list in it.

                      A Offline
                      A Offline
                      Anonymous_Banned275
                      wrote on last edited by
                      #17

                      @Chris-Kawa ```

                      Found it and as expected it was a very stupid mistake.

                      I have changed the array to a class available, but I was passing it indexed - as local variable - to my function.

                      Hence when I passed index 1 it became FIRST entry - 0 - in my test iteration...

                      Chris , thanks very much for all you help.

                      Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
                      "void MainWindow::menu_slot_indexed(int)"
                      "add it as subwindow "
                      "Run bluetootctl... \n void MainWindow_Bluetooth::on_actionBluetooth_service_enable_triggered()"
                      "void MainWindow_Bluetooth::Process_Menu_Index(int)"
                      ("Controller", "Agent registered") ("Discovery started", "SPP_CA") ("Controller", "Agent registered")
                      "1"
                      "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
                      ("Discovery started", "SPP_CA") ("Controller", "Agent registered") ()
                      "void MainWindow_Bluetooth::Process_Menu_Index(int)"

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved