1. The role of c_str() method is not really clear to me when it says:
The c_str() converts the contents of a String as a C-style, null-terminated string. It returns a pointer to the C-style version of the invoking String. Would appreciate to see the answer of the following question with example:
What is the difference between C-style string and String-style string?
2. I experimented the following codes to understand the role/application of c_str() method without any conclusion.
void setup()
{
Serial.begin(9600);
String myString = "Forum";
char *ptr = myString.c_str();//ptr holds base address of array not of type String
Serial.println(*(++ptr)); //shows: o (next character after F of Forum)
}
void loop()
{
}
The following code @UKHeliBob of post #10 works well even you enter 5 or more characters (including Newline character or carriage return character) from the Serial Monitor when the comparison will be made only with three character string red.
1. You are sending r e d Newline characters from Serial Monitor. The readString() method appends null-character, A null-character is automatically added at the end. Now, the String variable red contains five characters: r e d Newline Null.
2. The following equality comparison fails for reasons explained in post #11.
if (red == "red")
3. The red.c_str() method solves the problem in the following ways: (1) The red.c_str() method converts the glued String-style string composed of red\n\0 into C-style string like: {'r', 'e', 'd', '\n', '\0'} where individual characters could be easily accessed by index/pointer (cptr) which is provided by the following code:
char *cptr = red.c_str();
The pointer variable cptr holds the base address
of the array space that contains the characters: 'r', 'e', 'd', '\n', '\0'
as individual accessible items.
(2) The strstr() function compares characters (one-by-one) of the array pointed by cptr with the source characters of red. When the matching occurs, the process ends and the following message appears on the Serial Monitor. (The Newline character (0x0A = '\n') and Null-character (0x00 = '\0') are never compared.)
$101=152
4.Sketch using c_str() method and strstr() function:
3. But there should be a null-character at the end of r e d Newline as it is String-type string, Then, who adds this null-character at the end of r e d Newline that OP has sent from Serial Monitor and got saved in String-type variable red?
You have made the same mistake again. The String is not zero terminated or rather, as it is an object of the String class it can only be used with the String functions anyway, so it does not matter whether it is zero terminated or not because those functions do not need to use the zero termination as an end of string marker in the same way that the C str* functions do
The zero termination is added by the c_str() function when it converts from a String to a string and the resultant string can thus be used by the C str* functions
But the following codes show the message Arduino on the Serial Monitor which implies (as I understand) that there is a null-character as the last element of "Arduino" that is assigned to a String-type variable mystring.
@UKHeliBob
Can I request that this thread be split! this discussion between you and @GolamMostafa is only adding noise to OPs actual request! (No offence intended! )
@sherzaad
The OP has probably got his solution. Now, we are discussing something that is in line with the sprint (the String Class) of the thread from which OP would be benefitted as well!
Let people take the opportunity of being educated from the resourceful person!
You have not explained the functioning of the strstr() function andd c_str() method to the OP; so, it has not added much to his knowledgebase. You have not also reacted to the following comment @UKHeliBob.
technically the String class does maintain a null termination and the c_str() method just returns the buffer.
const char* c_str() const { return buffer; }
This is of course an implementation detail and the whole idea of classes is to abstract and hide the underlying implementation. You should not have to assume the String class does actually manage a cString internally.
for the first line you have an initializer that is a cString (so null terminated) as per C++ standard. But nothing tells you (implementation details) that this null char is copied over when the String is instantiated.
for the second line It just means that the println() method exposed by Serial (inherited from the Print class) can handle a String object. The way it manages that is unknown unless you look into the source code.
so it requests a cString compatible buffer from the instance and writes length bytes out. So technically you don't even need to know if the c_str() element is null terminated or not as the right count of bytes is passed to write()
As a side effect, this could let you add null characters into the String buffer and the print would not stop at the null char. Something the String class had to deal with.
What is the difference between C-style string and String-style string?
a C-style string also known as cString is a null terminated char array. the standard C libraries offers functions to work on those, see stdlib.h and string.h
a String style string is an instance of the String class and you don't have to know how the class represents the text, you just use the String class methods to handle that text. those methods are described in the class documentation
Should there be a null-character at the end of a string to print it. Does this null-character marks the end of string and thus stops the printing process?
In MSDOS's int 21h (09h) Programming, it was necessary to terminate the string by '$' (24h) to mark the end-of-string; otherwise printing process would continue for ever!