I am trying to understand char arrays in the arduino, but my code is showing some strange results. Can anyone help:
typedef struct {char TAG[12];} stTAGdef12;
typedef struct {char TAG[13];} stTAGdef13;
stTAGdef12 st12a;
stTAGdef12 st12b;
stTAGdef13 st13a;
stTAGdef13 st13b;
void setup()
{
// 01234567890123
Add_s12("012345678901");
Add_s13("0123456789012");
Add_a12("012345678901");
Add_a13("0123456789012");
Serial.begin(9600);
}
void loop()
{
Serial.print("\n12a >");Serial.print(st12a.TAG);Serial.print("<");
Serial.print("\n13a >");Serial.print(st13a.TAG);Serial.print("<");
Serial.print("\n12b >");Serial.print(st12b.TAG);Serial.print("<");
Serial.print("\n13b >");Serial.print(st13b.TAG);Serial.print("<");
Serial.print("\n12a if ");if (strcmp(st12a.TAG,"012345678901") == 0) Serial.print("Worked!!"); else Serial.print ("Failed");
Serial.print("\n13a if ");if (strcmp(st13a.TAG,"0123456789012") == 0) Serial.print ("Worked!!"); else Serial.print ("Failed");
Serial.print("\n12b if ");if (strcmp(st12b.TAG, "012345678901") == 0) Serial.print ("Worked!!"); else Serial.print ("Failed");
Serial.print("\n13b if ");if (strcmp(st13b.TAG, "0123456789012") == 0) Serial.print ("Worked!!"); else Serial.print ("Failed");
while(1){}
/* Results
connecting at 9600
12a >01234567890< (0-10)
13a >012345678901< (0-11)
12b >012345678901012345678901< ??
13b >0123456789012< (0-12)
12a if Failed
13a if Failed
12b if Failed
13b if Worked!!
*/
}
void Add_s12(String input){
input.toCharArray(st12a.TAG,12);
}
void Add_s13(String input){
input.toCharArray(st13a.TAG,13);
}
void Add_a12(String input){
for(int i=0;i<12;st12b.TAG[i]=input[i++]){}
}
void Add_a13(String input){
for(int i=0;i<13;st13b.TAG[i]=input[i++]){}
}
Eventually I intend to save the data structures to the EEPROM as therefore cannot use strings. I do not understand what is going on with 12b, have I missed something?
Thanks in advance.
Online Version: http://ift.tt/1Gdirah Target controller=Microview
Aucun commentaire:
Enregistrer un commentaire