# Reference to a String Array:

**URL:** <https://forum.arduino.cc/t/reference-to-a-string-array/525841>\
**Category:** Programming\
**Created:** [May 13, 2018, 2:15pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841 "2018-05-13T14:15:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snestrup2016](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/snestrup2016/32/1144431_2.png) [@snestrup2016](https://forum.arduino.cc/u/snestrup2016)\
**Post date:** [May 13, 2018, 2:15pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/1 "2018-05-13T14:15:27Z")

</div>

HI,

I have this pseudo/test code:

(Sorry for NOT Marking the code below, I have seearched for the CTL ????? (mark key(s)) in help, but I Just can't find it. Deeply sorry )

String Destin[] = {"WR","RD","DIR","ANA","PULL"};  
String Command[] = {"GO","STOP","HELP"};

void setup()  
{  
Serial.begin(9600);  
}

void loop()  
{  
.....

String Str = "GO";

Str.toUpperCase();

for (int n = 0; n \<= 3; ++n)  
{  
if (Command[n] == Str)  
{  
Serial.println('Command found ');

}  
else  
{  
Serial.println('Command not found ');

}  
}

.....

It does not work as I hoped. Instead I get a random number (error number (?)) written 3 times (Serial.println(..)) .

Question:

Is it possible in ARD C to do something as shown (modifying my code) or how should I do IT ?

Kris AKA Snestrup2016

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 13, 2018, 2:34pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/2 "2018-05-13T14:34:05Z")

</div>

Please [read this before posting a programming question](http://forum.arduino.cc/index.php?topic=97455.0) and take the advice regarding the use of code tags then post a complete program that can be compiled that demonstrates the problem.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 13, 2018, 2:36pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/3 "2018-05-13T14:36:41Z")

</div>

```cpp
 for (int n = 0; n <= 3; ++n)
 {
    if (Command[n] == Str)

```

Command has only three elements; don't try retrieving the fourth.

---

<div class="post-metadata">

**Author:** ![snestrup2016](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/snestrup2016/32/1144431_2.png) [@snestrup2016](https://forum.arduino.cc/u/snestrup2016)\
**Post date:** [May 13, 2018, 2:58pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/4 "2018-05-13T14:58:15Z")

</div>

HI,

#UKHeliBob : Thanks for the advice. Actually I knew it. MY fault. I apologize.

#AWOL: Correct. A big, big TYPO. Sorry, I should have discovered that - before I send the code. I was a bit frustrated, working on the code since early morning.

Here is the code (total):

String Istr;  
byte \_comma\_count = 0;  
String msg\_fields[20] = "";  
int Antal; /\* danish for NUMBER \*/  
String Command[] = {"WR","RD","HELP"};

void setup()  
{  
Serial.begin(9600);  
}

void loop()  
{  
String Str = "WR";

Istr = readline();  
if (Istr != "")  
{  
Istr.toUpperCase();  
Antal = getCSVfields(Istr);

//  
// this is a test , trying to make it easier to find and manipulate string(s).  
//

for (int n = 0; n \<= 2; n++)  
{  
if (Command[n] == Str)  
{  
Serial.println('Command found ');

}  
else  
{  
Serial.println('Command not found');

}  
}

/\*  
if (Antal != 0 )  
{  
Serial.print("antal = ");  
Serial.println(Antal);  
Serial.print("comm[0] = ");  
Serial.println(msg\_fields[0]);  
}

String Comm = msg\_fields[0];

Serial.print("NY comm = ");  
Serial.print(msg\_fields[0]);  
Serial.print(" '");  
Serial.print(Comm);  
Serial.print("' '");  
Serial.print(Comm[0]);  
Serial.println("' ");  
\*/

//  
// Tried if (msg\_fields[0] = "RD") { } ... does not work  
//  
// this works:

if ((0x52 == msg\_fields[0][0]) && (0x44 == msg\_fields[0][1]))  
{  
RDCommand();  
}  
else if ((0x57 == msg\_fields[0][0]) && (0x52 == msg\_fields[0][1]))  
{  
WRCommand();  
}  
Antal = 0;  
Istr = "";  
}  
}

String readline()  
{  
if (Serial.available() == 0) return ("");  
Istr = "";  
Istr = Serial.readStringUntil('\n'); /\* \r == CR \n = LF \*/  
return (Istr);  
}

int getCSVfields( String input\_buffer)  
{  
byte \_sentencePos = 0;

\_comma\_count = 0;  
for (int n = 0; n \<= 20; ++n)  
{  
msg\_fields[n] = "";  
}  
while (1)  
{  
if (input\_buffer[\_sentencePos] == NULL) break;  
if (input\_buffer[\_sentencePos] == 44) /\* , \*/  
{  
\_comma\_count++;  
msg\_fields[\_comma\_count] = "";  
\_sentencePos++;  
}  
else  
{  
msg\_fields[\_comma\_count] += input\_buffer[\_sentencePos];  
\_sentencePos++;  
}  
}  
return \_comma\_count + 1;  
}

void RDCommand()  
{  
Serial.println("test RD 1 ");  
Serial.print("test COMM 2 ");  
Serial.println(msg\_fields[1]);

return;  
}

void WRCommand()  
{  
Serial.println("test WR 1 ");  
Serial.print("test COMM 2 ");  
Serial.println(msg\_fields[1]);  
return;  
}

Why do I have to use Integers as chars in

else if ((0x57 == msg\_fields[0][0]) && (0x52 == msg\_fields[0][1])) --- this ?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 13, 2018, 3:00pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/5 "2018-05-13T15:00:41Z")

</div>

> #UKHeliBob : Thanks for the advice. Actually I knew it. MY fault. I apologize.

Then why did you decide to ignore the advice again ?

---

<div class="post-metadata">

**Author:** ![snestrup2016](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/snestrup2016/32/1144431_2.png) [@snestrup2016](https://forum.arduino.cc/u/snestrup2016)\
**Post date:** [May 13, 2018, 3:14pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/6 "2018-05-13T15:14:25Z")

</div>

HI,

Yes, You may say i am a fool, and In some ways you're right. (Please don't say it loudly !).

I have printed the approx. 15 pages and I can assure you, that I WILL read it again. Carefully and concentrated.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 14, 2018, 1:19pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/7 "2018-05-14T13:19:18Z")

</div>

> Why do I have to use Integers as chars in
> 
> else if ((0x57 == msg\_fields[0][0]) && (0x52 == msg\_fields[0][1])) --- this ?

You don't. 0x57 is the same as 'W' and 0x52 is the same as 'R'.

```arduino
  for (int n = 0; n <= 20; ++n)
  {
    msg_fields[n] = "";
  }

```

Crapping on element 21 of a 20 element array is still NOT a good idea.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:11pm UTC](https://forum.arduino.cc/t/reference-to-a-string-array/525841/8 "2021-05-05T23:11:17Z")

</div>

Closed on May 5, 2021, 11:11pm UTC


