Home AMX User Forum NetLinx Studio
Options

Using char arrays to create new array

Ok, this is driving me up the wall! Hopefully someone can point me in the right direction...

How do I create an array built up of char array variables/constants? So if I have the variables:
DEFINE_VARIABLE  //have tried this with constant as well
char STRING_1[] = 'hello'
char STRING_2[] = 'goodbye'
char STRING_3[] = 'this is a test'

How do I then create an array from them in a function, something like:
DEFINE_FUNCTION myFunction(){
	stack_var char myArray[2][14];
	myArray = {STRING_1,STRING_2};
}

The above won't compile, but is what I'd like to do. What will compile is this, although it's a bit long winded for what should be a simple thing to do:
DEFINE_FUNCTION myFunction(){
	stack_var char myArray[2][14];
	myArray[1] = STRING_1;
	myArray[2] = STRING_2;
}

However, if you then do length_array(myArray) you get 0.

What am I doing wrong?

Comments

  • Options
    trobertstroberts Posts: 228
    This is how I would define it...does this help?


    Code:
    DEFINE_VARIABLE
    VOLATILE CHAR STRING[3][14]

    DEFINE_START
    STRING[1] = 'hello'
    STRING[2] = 'goodbye'
    STRING[3] = 'this is a test'
  • Options
    viningvining Posts: 4,368
    An array will only have lenght (lenght_array) if it is initialized in define_variable. If is not initialized in define_variable you can use max_length_array to returned the defined array length or you have to use set_length_array when and where ever it is initialized or modified.

    In your case even though it has no length it still holds the values you gave it but since since you didn't set the length calling length_array on it will always return 0 unless initialized where defined and that only works on global vars.

    It doesn't make sense to me either but it is what it is.
  • Options
    ijedijed Posts: 28
    tom82 wrote: »
    [/code]

    How do I then create an array from them in a function, something like:
    DEFINE_FUNCTION myFunction(){
    	stack_var char myArray[2][14];
    	myArray = {STRING_1,STRING_2};
    }
    

    try using string literals - this should work

    myArray = {"STRING_1","STRING_2"};
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    An array will only have lenght (lenght_array) if it is initialized in define_variable.

    That is not a true statement as the following code demonstrates:
    DEFINE_DEVICE
    dvTP = 10001:1:0
    
    DEFINE_CONSTANT
    CHAR cHey[] = 'Hey'
    CHAR cThere[] = 'There'
    
    DEFINE_VARIABLE
    CHAR cUnInitialized_1[16]
    CHAR cUnInitialized_2[16]
    
    DEFINE_EVENT
    BUTTON_EVENT[dvTP,1] {
       PUSH: {      
          // Length will be set by this assignment
          cUnInitialized_1 = "cHey,' ',cThere"
          SEND_STRING 0,"'LENGTH_ARRAY(cUnInitialized_1) = ',ITOA(LENGTH_ARRAY(cUnInitialized_1))"
       }
    }
    
    BUTTON_EVENT[dvTP,2] {
       PUSH: {
          // Poking individual cells so length will not be altered with these statements
          cUnInitialized_2[1] = 'H'
          cUnInitialized_2[2] = 'e'
          cUnInitialized_2[3] = 'y'
          SEND_STRING 0,"'LENGTH_ARRAY(cUnInitialized_2) = ',ITOA(LENGTH_ARRAY(cUnInitialized_2))"
       }
    }
    

    And the output when buttons 1 and 2 are pushed:
    Line      1 (23:31:32)::  LENGTH_ARRAY(cUnInitialized_1) = 9
    Line      2 (23:31:35)::  LENGTH_ARRAY(cUnInitialized_2) = 0
    
  • Options
    viningvining Posts: 4,368
    Yep, you're right. I think I had init'ing button arrays in define_start stuck in my head from a few weeks ago and that would basically be "poking" as you put one element at a time too. Everything I just tried other than adding to the array one element at a time worked perfectly fine whether it was a string literal or an expression.

    So to the OP you're not getting length because you're "poking" one element at a time.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    Yep, you're right. I think I had init'ing button arrays in define_start stuck in my head from a few weeks ago and that would basically be "poking" as you put one element at a time too. Everything I just tried other than adding to the array one element at a time worked perfectly fine whether it was a string literal or an expression.
    The reported length will still change even if you add to the array one element at a time. If you simply modify an element in an array then the length will not change.

    As an aside, Peek and Poke stuck with me from my programming days back when dinosaurs roamed the earth.

    Poke = write to an address in memory.
    Peek = read an address in memory.
  • Options
    viningvining Posts: 4,368
    Joe Hebert wrote: »
    The reported length will still change even if you add to the array one element at a time. If you simply modify an element in an array then the length will not change.
    Unfortunately I wasn't programming when I too roamed with the dinasours so my brain hasn't had the chance to evolve in respect to programming, not sure its evolved much at all in respect to anything but that's another sad story. Now I think I'm actually de-evolving which shouldn't even be possible since I never really evolved in the first place.

    So if an array is defined with a max length somewhere I'm not getting the difference between adding and modifying, wouldn't everything after the var being defined be considered modifying even the first time an element is populated?
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    vining wrote: »
    So if an array is defined with a max length somewhere I'm not getting the difference between adding and modifying, wouldn't everything after the var being defined be considered modifying even the first time an element is populated?

    The difference is modifying an element...a single element...as shown in the button 2 example above.

    If you modify a single element in an array (whether it’s the first time or not) then the length will not change.
Sign In or Register to comment.