UKHeliBob makes a good point. Allow me to elaborate:
The variable data, passed to the function process_data(), is declared as a pointer to char, which will be used to iterate an array of chars.
An array of chars can, if null terminated, be used as a C string, but it is not a String object. Also, an array of chars can be used to instantiate a String object, like this:
char my_char_array[] = "Jabber jabber!"; // A string constant initializing an array of chars
String my_string = String(my_char_array); // An instantiation of a String object from an array of chars
Since data is being compared to a string constant, such as "SW0013A200400A11115", and since data is a pointer to an array of chars [see Note 1, below], one must use something like the C-library function strcmp() [from string.h] to do the comparison.
char string1[] = "SW0013A200400A11114";
char string2[] = "SW0013A200400A11115";
char string3[] = "SW0013A200400A11116";
void process_data (const char * data)
{
Serial.print("data:\"");
Serial.print(data);
Serial.println("\"");
if (strcmp(data, "SW0013A200400A11114") == 0)
{
Serial.println("-- Is string 1");
}
else if (strcmp(data, "SW0013A200400A11115") == 0)
{
Serial.println("-- Is string 2");
}
else
{
Serial.println("-- Is not recognized!");
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
process_data(string1);
process_data(string2);
process_data(string3);
while(1);
}
/***************************************
* Produces the following output: *
***************************************
data:"SW0013A200400A11114"
-- Is string 1
data:"SW0013A200400A11115"
-- Is string 2
data:"SW0013A200400A11116"
-- Is not recognized!
***************************************/
This can also be done with String objects like this:
char string1[] = "SW0013A200400A11114"; // Using a string constant to initialize an array of chars. Null termination occurs in the string constant.
String String1 = String(string1); // Instantiation from existing array of chars
String String2 = String("SW0013A200400A11115"); // Instantiation from string constant
String String3 = String("SW0013A200400A11116");
void process_data (String data)
{
Serial.print("data:\"");
Serial.print(data);
Serial.println("\"");
if (strcmp(data.c_str(), "SW0013A200400A11114") == 0) // Doing it with a C Library function
{
Serial.println("-- Is string 1");
}
else if (data.compareTo("SW0013A200400A11115") == 0) // Doing it with a String method
{
Serial.println("-- Is string 2");
}
else
{
Serial.println("-- Is not recognized!");
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
process_data(String1);
process_data(String2);
process_data(String3);
while(1);
}
// The output is the same as before!
Note 1: if the array of chars that data is pointing to is not null terminated, one would think there's the possibility of Buffer Overrun -- BUT, The Arduino compiler seems to have some bad a$$ intelligence for catching this, 'cuz I tried to cause buffer overrun, and not only did the compiler issue an "undefined behavior" warning, but the code was actually altered to insure no overrun can happen -- though the code may not behave as intended -- pretty cool! Here's the code I wrote to attempt a Buffer Overrun:
char string1[5]; // Notice: only 5 elements allocated
String String1;
String String2 = String("SW0013A200400A11115"); // Instantiation from string constant
String String3 = String("SW0013A200400A11116");
void process_data (String data)
{
Serial.print("data:\"");
Serial.print(data);
Serial.println("\"");
if (strcmp(data.c_str(), "SW0013A200400A11114") == 0) // Doing it with a C Library function
{
Serial.println("-- Is string 1");
}
else if (data.compareTo("SW0013A200400A11115") == 0) // Doing it with a String method
{
Serial.println("-- Is string 2");
}
else
{
Serial.println("-- Is not recognized!");
}
}
void setup() {
Serial.begin(9600);
// Try to cause a buffer overrun by assigning more than 5 chars
// to string1 (which was declared with room for only 5 chars).
// Also, I intentionally did NOT null terminate the thing!
// Buffer overrun is expected here.
for (int i=0; i<10; ++i)
{
string1[i] = 'a';
}
String1 = String(string1); // The buffer overrun could, also, occur here (because string1 was declared to hold only 5 chars, and the 5th char is an 'a' and not a '\0')!
}
void loop() {
process_data(String1);
process_data(String2);
process_data(String3);
while(1);
}
/***************************************
* Produces the following output:
* Notice how the string in data has
* expanded to contain all 10 'a's
***************************************
data:"aaaaaaaaaa"
-- Is not recognized!
data:"SW0013A200400A11115"
-- Is string 2
data:"SW0013A200400A11116"
-- Is not recognized!
***************************************/
Even when I tried it on the C-Lib strcmp() function, it still survived (with warning)!
char string1[5]; // Notice: only 5 elements allocated
char string2[] = "SW0013A200400A11115";
char string3[] = "SW0013A200400A11116";
//String my_string = String(my_char_array);
void process_data (const char * data)
{
Serial.print("data:\"");
Serial.print(data);
Serial.println("\"");
// Buffer Overrun could, also, happen here:
if (strcmp(data, "SW0013A200400A11114") == 0)
{
Serial.println("-- Is string 1");
}
else if (strcmp(data, "SW0013A200400A11115") == 0)
{
Serial.println("-- Is string 2");
}
else
{
Serial.println("-- Is not recognized!");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Try to cause a buffer overrun by assigning more than 5 chars
// to string1 (which was declared with room for only 5 chars).
// Also, I intentionally did NOT null terminate the thing!
// Buffer overrun is expected here.
for (int i=0; i<10; ++i)
{
string1[i] = 'a';
}
}
void loop() {
process_data(string1);
process_data(string2);
process_data(string3);
while(1);
}
/***************************************
* Produces the following output: *
***************************************
data:"aaaaaaaaaa"
-- Is not recognized!
data:"SW0013A200400A11115"
-- Is string 2
data:"SW0013A200400A11116"
-- Is not recognized!
***************************************/
BTW: The Arduino IDE includes string.h in the background, so no need to add an include statement to your code.
BTW2: instantiate is a fancy word for creating an object from a class. Though, String is a special case, because it's actually a language construct, but, more than likely, behind the scenes, there is an instantiation going on.