Page 1 of 1

String Array Clear

Posted: Thu Nov 12, 2020 4:42 pm
by Alan_37
Hi is there a function to flush / Clear a string Array

I know I can do Array[0] = 0 and so on or a loop that goes true all the array and set
all the elements to 0.

I was hoping something more simple like Array = "" which does not work
also tried a c# icon with Array.Clear(FCV_Array, 0, FCV_Array.Length);
also does not work

Any suggestions, Please

Re: String Array Clear

Posted: Thu Nov 12, 2020 5:05 pm
by mnf
Hi Alan,

What do you actually have - do you have an array of strings or just a single string?

If it's just a single string - setting s[0] = 0 effectively marks it as a null string.

You could use a loop - or use memset (in a C block) - though this is effectively a loop.
(for(I = 0; I < sizeof(s); I++) s = 0;) You can get the array size if you use a macro....
(Note that some implementations 'might' optimise this to use word or long writes - but probably not)

If it's an array of strings - you can still clear the whole block in a single loop.
(for(I = 0; I < sizeof(s) * numberOf(s); I++) …. in pseudocode)

Martin

Re: String Array Clear

Posted: Thu Nov 12, 2020 5:08 pm
by Benj
Hello,
If it's just a single string - setting s[0] = 0 effectively marks it as a null string.
This would also do the same thing, but only works for a single string.

Array = ""

If you have a 2D array then you would have to cycle through each string value.

Array[idx][0] = 0
or
Array[idx] = ""

Where idx is 0 to array size -1.

Re: String Array Clear

Posted: Thu Nov 12, 2020 5:22 pm
by Alan_37
Hi, it is just a 1d Array that is storing bytes from an sd card file.
Array = "" dose not work .

I just need to clear it as my problem is when for example first I populate the array with "123456789"
then if I use it again to have it populated with "abc" I get a result of "abc456789"

the only way I found that works is getting the Array length to a variable and loop to set elements to 0 ( Array[idx] =0 )

I also tried to use a circular buffer for this but the data get erased after I read from it, for some reason is not permanent
until i flush

Re: String Array Clear

Posted: Thu Nov 12, 2020 6:00 pm
by mnf
Looks like the assignment of 'abc' - needs to add a '\0' (end of string or EOS) character s[3]=0 - can you post your code that is doing the assignment?

Martin.