Using char arrays to create new array
tom82
Posts: 17
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:
How do I then create an array from them in a function, something like:
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:
However, if you then do length_array(myArray) you get 0.
What am I doing wrong?
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?
0
Comments
Code:
DEFINE_VARIABLE
VOLATILE CHAR STRING[3][14]
DEFINE_START
STRING[1] = 'hello'
STRING[2] = 'goodbye'
STRING[3] = 'this is a test'
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.
try using string literals - this should work
myArray = {"STRING_1","STRING_2"};
That is not a true statement as the following code demonstrates:
And the output when buttons 1 and 2 are pushed:
So to the OP you're not getting length because you're "poking" one element at a time.
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.
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.