Home AMX User Forum NetLinx Studio

LENGTH_ARRAY on Structure array

Having the following code:
DEFINE_STRUCTURE
STRUCTURE TMyStruct
{
  CHAR cStr1[10];
  CHAR cStr2[5];
}

DEFINE_VARIABLE
TMyStruct MyStruct[3];

DEFINE_START
MyStruct[1].cStr1 = 'Test1';
MyStruct[1].cStr2 = '^^';

when i use LENGTH_ARRAY(MyStruct) i get always zero. I was expected to have a value of 1 but...nope.
Now i have to find the active length either by looping through the array or by keeping an index.

Another thing with arrays is that i cannot Increase the maximum length during run-time. In the Help file, somewhere, is written:
"LENGTH_ARRAY returns the effective length of a dimension of an array: the length set implicitly through array initialization or ARRAY MANIPULATION OPERATIONS (+ and –) or explicitly through a call to SET_LENGTH_ARRAY" which made me think that i could do it.

....i have given up trying to make it happen, so i rest my thoughts in the forum...

Kostas

Comments

  • AMXJeffAMXJeff Posts: 450
    papadouk wrote: »
    Having the following code:
    DEFINE_STRUCTURE
    STRUCTURE TMyStruct
    {
      CHAR cStr1[10];
      CHAR cStr2[5];
    }
    
    DEFINE_VARIABLE
    TMyStruct MyStruct[3];
    
    DEFINE_START
    MyStruct[1].cStr1 = 'Test1';
    MyStruct[1].cStr2 = '^^';
    

    when i use LENGTH_ARRAY(MyStruct) i get always zero. I was expected to have a value of 1 but...nope.
    Now i have to find the active length either by looping through the array or by keeping an index.

    Another thing with arrays is that i cannot Increase the maximum length during run-time. In the Help file, somewhere, is written:
    "LENGTH_ARRAY returns the effective length of a dimension of an array: the length set implicitly through array initialization or ARRAY MANIPULATION OPERATIONS (+ and –) or explicitly through a call to SET_LENGTH_ARRAY" which made me think that i could do it.

    ....i have given up trying to make it happen, so i rest my thoughts in the forum...

    Kostas

    In order to use LENGTH_ARRAY, the structure array must have length by using SET_LENGTH_ARRAY. SO... Instead... use MAX_LENGTH_ARRAY. This returns the upper bounds of the array indicating this is the max length the array can be.


    On the same front... you must always have a max array length. What you do to expand... is declare the array to the max size you will ever need, and use SET_LENGTH_ARRAY to indicate how much you are using, than you can use LENGTH_ARRAY and virtually expand it by using SET_LENGTH_ARRAY again.
    somestruct MyStruct[100];
    
    SET_LENGTH_ARRAY(MyStruct, 10);
    
    length = LENGTH_ARRAY(MyStruct) // length = 10
    
    maxLength = MAX_LENGTH_ARRAY(MyStruct) // maxlength = 100
    
    
  • DHawthorneDHawthorne Posts: 4,584
    If you directly assign values to an array, LENGTH_ARRAY will not return the size of the used portion of the array. It only gets updated when you use a function or string operator to update the array. It's mainly intended for strings. If you set the array bounds to, let's say, 25, then do something like, "srtTestArray = 'test string'", then run LENGTH_ARRAY, you'll get 11. If you subsequently run SET_LENGTH_ARRAY(strTestArray, 4), your array will now have the value 'test' in any string operation you run on it ... but if you looked at strTestArray[6], you would see the 's' is still there.

    So, in effect, these functions don't deal with the absolute values in the array, but the effective ones.
  • Thank you Jeff and Dave (in order of appearance).

    All the things you described are included in the way i work with arrays. It has been like this since i step my foot in the deep waters of Netlinx. It just hit me one of these days to get the active length of a struct by using LENGTH_ARRAY instead of the pre - mentioned ways...It didn't work, so i'm back in my hole.

    About the Dynamic growing of an array (beyond the MAX length of it's declaration) was a 'C' programming artifact that hounded me..

    :)
Sign In or Register to comment.