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 ?
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.
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.
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.
Ginza:
Hi septillion, I think im obsessive with good code practices
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.
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
And no, you can't clear the whole array in one line, not without a function. But you can try
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
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.