Strange behaviour of a char array

Hi all!

I’m having a strange behaviour with a char array.

Here is my code:

...
for(uint8_t i = 0; i < numberOfDevices; i++) {

            	Serial.print(F("Sonda número: "));
            	Serial.println(i, DEC);

            	Serial.print(F("Identificador: "));
            	printAddress(tempDevAddON[i]);
            	Serial.println();

            	Serial.print(F("Alias: "));

            	for (uint8_t j = 0; j < 6; j++) {

            		Serial.write(probesAliasON[i][j]);
            		pAliasONLocal[j] = probesAliasON[i][j];

            	}

                Serial.println();
            	Serial.println(pAliasONLocal);
            	Serial.println();
...

These two arrays are defined as follows:

char pAliasONLocal[6];
char probesAliasON[N_MAX_ON][6];

Where N_MAX_ON is:

#define N_MAX_ON 10

And the strange thing is that I have the following output:

Sonda número: 0
Identificador: 28FF8843B3170151
Alias: sondaA
sondaA.txt
...
Sonda número: 1
Identificador: 28FFD243B3170179
Alias: sondaB
sondaB.txt

My questions:

1 - Why an array that has a dimension of 6 (char pAliasONLocal[6]) can store sondaA.txt? Because sondaA.txt has 10 characters…

2 - Why this array is getting this .txt?

I need your help, I’m getting crazy!!!

TY

My questions:

1 - Why an array that has a dimension of 6 (char pAliasONLocal[6]) can store sondaA.txt? Because sondaA.txt has 10 characters…
It can’t. You wrote beyond the end of the array.

I need your help, I’m getting crazy!!!

Stop doing crazy things. Pay attention to which dimension should size the second array.

PaulS:
My questions:

1 - Why an array that has a dimension of 6 (char pAliasONLocal[6]) can store sondaA.txt? Because sondaA.txt has 10 characters...
It can't. You wrote beyond the end of the array.
Stop doing crazy things. Pay attention to which dimension should size the second array.

First: I'm not doing crazy things. I have a matrix where I store all of the probes alias. The matrix is as follows:

sondaA
sondaB
sondaC
...
sondaJ

(10 elements)

Second: As you see, with a for I print all this elemens (sondaA, sondaB, sondaC, ...) and I copy them into a momentary char array because I have to manipulate them individually (I have to use the sprintf function to construct a 'path' because I have to create an individual directory for each element. Using a momentary char array for doing it is more eazy). And finally my problem: I print in each cicle the value of the char pAliasONLocal[6]. As you see, instead of printing sondaA or sondaB it prints sondaA.txt or sondaB.txt. It have no sense because it's a char array[6] (and why it prints '.txt' and not 'was' or 'asf'?).

Anyone can help me?

How many characters in e.g. the word sondaA? How many bytes do you need to store the word sondaA in a nul-terminated character array?

First: I'm not doing crazy things.

You are if you think you can store 6 characters plus a NULL in a 6 element array.
You are if you think that you can treat a 6 element char array as a string, without the NULL terminator.

sterretje:
How many characters in e.g. the word sondaA? How many bytes do you need to store the word sondaA in a nul-terminated character array?

All the words that I have to store have 6 characters. So I need 6 bytes to store 'sondaA'. Because of this, I SHOULD use a 7 element array because of the NULL terminator (I'm sorry, I didn't remember it).

PaulS:
You are if you think you can store 6 characters plus a NULL in a 6 element array.
You are if you think that you can treat a 6 element char array as a string, without the NULL terminator.

You're right. Sorry :frowning: