Array question...

Hi everybody, I have the following question...when we declare and Array it can be initialized like this:

char taux[10] = {'\0'};

Several lines then, after stored some data into the array, I want re-initialized it, but with this sentence Im changing the last index of the array or it affects all the array elements ?

taux[10] = {'\0'};

Best regards, Ginza.

You're not changing the last element, you're writing past the end of your array.
Oops.

char taux[10] = {'\0'};

defines an array of 10 items, from taux[0] through taux[9].

taux[10] = {'\0'};

sets an item to a value of zero, but this item is past the defined end of the array and will zero out whatever follows it in memory... probably not what you intended.

To empty the string you need to just call

taux = '\0';

Calling taux[10] is indeed writing past the array (it's 10 in size so taux[0] to taux[9]).

This will make taux[0] to be 0 (\0 s the ascii NULL pointer aka 0). It doesn't matter this only changes the first entry because in C++ a string is NULL terminated. If a NULL is found in the string (is the same as a 0 in the array) the string is at it's end.

Hi everybody, thanks for your replies.

AWOL, I wanted to write:

char taux[10] = {'\0'};

taux[9] = {'\0'};

Sorry for the confussion.

I want re-initialize the array with one line code. Is it posible ?

septillion, If you use double quotes the String class is called in your example ? I ask you because Im trying to avoid strings variables.

Best regards, GInza.

If your array is a null terminated char array, then it's called a C string. The string ends where the null character is. It's not an object of the String class.

To "reset" a C string, do

char array[] = "hello";

array[0] = '\0';

Of course, this only overwrite the first element of this array. array[1] will still contain 'e', but if you try Serial.print( array ) it would show nothing.

If you want to reset all the content of the array, you can use memset.

No, double quotes don't call the string class. But you are right, I should have use single quotes.

Re-initiating a C string string (not the String class) as in, an empty string, is as simple as putting a 0 in the first entry of the array. You can do this in two ways in one line

taux[0] = '\0'; //or
taux[0] = 0;

The rest of the 9 entries are not emptied but that's not a problem. A string ends at the first 0 (aka NULL char) that is found. The rest can be anything.

Hi everybody, thanks for the replies.

I will test memset, if it doesnt work like I hope, I will write a little function to pass a char array and clear all the elements.

Best regards, Ginza.

Why DO you want to desperately clear the whole array? I don't see the point...

As if the initialization with char taux[10] = {'\0'} also only initializes the first entry of the array... And that's enough for C++ type strings.

Hi septillion, I think im obsessive with good code practices :slight_smile:

You know that when you have the code under control is better.

Best regards, Ginza.

As if the initialization with char taux[10] = {'\0'} also only initializes the first entry of the array...

Is that true for global arrays or static arrays declared within a function ?

Ginza:
Hi septillion, I think im obsessive with good code practices :slight_smile:

You know that when you have the code under control is better.

Best regards, Ginza.

When you do things that you know should have no effect other than wasting processor time, that is not gaining any control. It means you don't have confidence in the code, which can be addressed better by careful coding and code review.

I agree with aarg, you only wast processor power to do nothing useful...

@UKHeliBob, if I would do the same with a int array

int myArray[10] = {8};

It would make an array of size 10 and only initialize the first entry to be 8. Why would it be any different if it's a char?

char myArray[10] = {'\0'};

Would create a array for 9 chars (+ one position for termination) and initialize the first entry to be NULL / 0 / '\0'.

Well, my goal was re-initialize a char array in one line code.

For example imagine that you have a char array and you want to store in each element a sensor status like One or Zero.

Perhaps in some part of the code you want to re-initialize the whole array to the same value and then store new values. Is only an idea, perhaps can be better examples.

It would make an array of size 10 and only initialize the first entry to be 8. Why would it be any different if it's a char?

It's not any different for chars and I don't think that I said it was.

When a global array or a static local array is declared but given no values then it is filled with zeroes. This is the case for arrays of any type of variable. If it is partially initialised then the subsequent levels of the array are filled with zeroes. This is usually only significant in the case of arrays of chars where your program may depend, probably wrongly, on the automatic filling with zeros correctly terminating a string.

Ginza:
Well, my goal was re-initialize a char array in one line code.

Leaving aside memory considerations, do you really care whether it takes one line or 10 ? Write a function to set each level of the array to zero and call it when needed. Alternatively, put the terminating zero in the right place when you put a new value in the array of chars.

Ahhhhhhhh, you don't want to use it as a string! Then don't make it a char! If you want to make a array for sensor data make it a byte or an int or a unsigned int etc.

Also, with char taux[10] = {'\0'} you only initialize the first entry of the array :wink:

And no, you can't clear the whole array in one line, not without a function. But you can try

#define clearIntArray(x) clearIntArrayFunc(x,sizeof(x)/sizeof(x[0]))

void clearIntArrayFunc(int *a, byte length){
  for(byte i = 0; i < length; i++){
    a[i] = 0;
  }
}

And use as

int myList[] = {1, 2, 3};
clearIntArray(myList);

Long live functions :slight_smile:

@UKHeliBob, Sorry, then I misunderstood your question.

The point was I thought he used the array as a string (and looking at the replies I was not the only one...).

Yeay, you can use a char if you need a signed 8-bit. It was more because it confused me. More because he initiated the array with an ascii character.

I think is very useful use a char array to store one byte data per index, also it can be posible with a byte array. Both ways are right

It depends in the needs of the system.

The "char" type implies it's going to hold characters. "char" can also be signed or unsigned (it depends on the implementation) unless it's explicitly specified with "signed" or "unsigned".

The Arduino "byte" type or the standard "uint8_t" type should be preferred for holding unsigned 8-bit data. (Or use the standard "int8_t" type for signed 8-bit data.)

septillion:
Also, with char taux[10] = {'\0'} you only initialize the first entry of the array :wink:

No, it fills the whole array with zeroes. The first is explicitly given with '\0' and the rest are initialized to zero because that's what C++ does.