Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"Improving the question "

Here is my array.
Is it correct syntax?
I need to get size of each subarray.
Then I like to retrieve specific subarray.





C++
// test array
   const QStringList list_array[10]
   {
       {   "SubWindow terminal all options         ",
           "SubWindow terminal configure           ",
           "SubWindow terminal setup               ",
           "SubWindow terminal connect  serial     ",
           "SubWindow terminal connect  Bluetooth  ",
           "SubWindow terminal DEBUG / TRACE       ",
           "SubWindow terminal connect  Bluetooth  ",
           "SubWindow terminal DEBUG / TRACE       ",
           "SubWindow terminal LAST member         "
       },
       { "SubsubWindow bluetoothctl help        (QMdiArea )",
           "SubsubWindow bluetoothctl list  (QMdiArea )",
           "SubsubWindow bluetoothctl devices (QMdiArea )",
           "SubsubWindow bluetoothctl show  (QMdiArea )" },
       { "SubWindow terminal     (QMdiArea )",
           "SubWindow hcitool      (QMdiArea )",
           "SubWindow bluetoothctl (QMdiArea )",
           "SubWindow system       (QMdiArea )" },
       { "SubsubWindow bluetoothctl help        (QMdiArea )",
           "SubsubWindow bluetoothctl list  (QMdiArea )",
           "SubsubWindow bluetoothctl devices (QMdiArea )",
           "SubsubWindow bluetoothctl show  (QMdiArea )" },
       { "SubsubWindow hcitool        (QMdiArea )",
           "SubsubWindow hcitool   dev  (QMdiArea )",
           "SubsubWindow hcitool   scan (QMdiArea )",
           "SubsubWindow hcitool   inq  (QMdiArea )",
           "SubWindow system  LAST array member      (QMdiArea )"
       }
   };



My test code

C++
for(int TEST = 0; TEST < 5; TEST++)
 {
     text = " test ARRAY size TEST ";
     text += QString::number (TEST );
     text += " ";
     text += QString::number (list_array[TEST][0].size());
     text += " sizeof ";
     text +=  QString::number (sizeof (list_array[TEST]));

             qDebug() << text;
      }






My test code result
C++
 // test
 " test ARRAY size TEST 0 39 sizeof 8"
" test ARRAY size TEST 1 49 sizeof 8"
" test ARRAY size TEST 2 34 sizeof 8"
" test ARRAY size TEST 3 49 sizeof 8"
" test ARRAY size TEST 4 39 sizeof 8"






With index = 0, the list_array[index].size(); return correct size of main (string) array,

with index = 0 and "sub array = 0 "
the list_array[index][0].size(); returns first member of main array count of characters.

I am after size of the first sub array.


C++
int subSize = list_array[index].size();
                int subsubSize = list_array[index][0].size();


What I have tried:

I did try to vary the index - but that seems silly....( just to satisfy the counter )
Posted
Updated 22-Jan-24 16:13pm
v4
Comments
merano99 19-Jan-24 21:49pm    
What is the declaration of list_array?
Richard MacCutchan 20-Jan-24 4:42am    
That will only work if list_array[index][0] is also a Qt array. If it is something else then you will need to use some other method that can understand the data structure.

Your question is not clear. This code works for me :
C++
const int dimOne   = 8;
const int dimTwo   = 10;
const int dimThree = 12;

typedef int ThreeD[ dimOne ][ dimTwo ][ dimThree ];
ThreeD array;
int sizeTwo = sizeof( array[ 0 ][ 0 ] );
printf( "sizeTwo is %d\n", sizeTwo );
I am not familiar with a class called array that has a size method. What library is it in?
 
Share this answer
 
Why are you using sizeof on a pointer? As you see it correctly tells you that each pointer is 8 bytes long. You need to use a function/method that gives you the length of the actual character string pointed to by list_array[TEST][0].
 
Share this answer
 
Comments
Salvatore Terress 23-Jan-24 10:22am    
Unfortunately that is not what I need - I need size , # , of members in subarray.
That is why I asked if my array definition is correct.

for(int TEST = 0; TEST < 5; TEST++)
{
text = " test ARRAY size TEST ";
text += QString::number (TEST );
text += " ";
text += QString::number (list_array[TEST][0].size());
text += " sizeof ";
text += QString::number (sizeof (list_array[TEST][0]));

qDebug() << text;
}

Output of code above:

" test ARRAY size TEST 0 39 sizeof 8"
" test ARRAY size TEST 1 49 sizeof 8"
" test ARRAY size TEST 2 34 sizeof 8"
" test ARRAY size TEST 3 49 sizeof 8"
" test ARRAY size TEST 4 39 sizeof 8"
Richard MacCutchan 23-Jan-24 10:48am    
But what you are referring to are not arrays, they are strings. You have a single array of QStringList items. And each list contains a number of strings. So list_array[TEST] refers to one of the lists. And list_array[TEST][0] refers to the first string in that list. See QStringList Class | Qt Core 6.6.1[^] for full details.
Salvatore Terress 23-Jan-24 11:29am    
Just update
I have this (working( example of single list I will try to adapt to "two level " list array

// test
QStringList fonts = { "Arial", "Helvetica", "Times" };
for (int i = 0; i < fonts.size(); ++i)
{
cout << fonts.at(i).toLocal8Bit().constData() <
Richard MacCutchan 23-Jan-24 11:57am    
I am not sure I understand exactly what you are trying to achieve.
SOLVED

list_array[index].size();

sorry OF forgot the solution he has received few weeks ago
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900