I'm wondering what is better. Using a "temporary" String to send text to a function.
-or-
Store the text in a C string and then send that to a function.
I'm using the UTFT library to display stuff to my screen.
The manual says send a String or C string (which is a char array) to the print function.
So if all I want to do is print the same text every time I click a button. Which of these uses less memory and uses the memory the most responsibly?
can't you use c_str() to convert a String to char*?
#include <string>
#include <iostream>
using namespace std;
int
main()
{
 string SS;  // C++ STL string
 SS = "This is a string";
 cout << SS << endl;
 printf ("%s: %s\n", __func__, SS.c_str());
}
Yes. But that's not what I'm asking. And in Arduino, you really shouldn't use String objects. You should use C string variables. But in my two examples, I'm not using a String object per say.
(Maybe read the blog post I shared in my original post? But I'm also still trying to understand this.)
If we had the amount of memory as a regular computer, String vs C string wouldn't matter all that much.
It's because we don't have a boat load of computing power or memory in an Arduino that makes this important.
I'm wondering if I should just send the actual String to the function without creating any String object. (Which causes the compiler to throw a minor error.)
Such as this:
myGLCD.print("Hello World!", 10, 150);
(Notice that I didn't actually use a String object. I just sent the text. If I was gathering text and then sending it, I would use a C string.)
OR
Is it better on saving memory and computing power to do this? Which is to create a C string variable and then pass that to the function (Because using C string is the proper way to use long groups of text in Arduino).
mafokken:
I'm wondering if I should just send the actual String to the function without creating any String object. (Which causes the compiler to throw a minor error.)
myGLCD.print("Hello World!", 10, 150);
This just reflects bad implementation of the library. "Hello World!" is a const char* but print() takes char* hence the warning/error.
If you don't want to modify the library (which you should), this is what you have to do.
mafokken:
Is it better on saving memory and computing power to do this? Which is to create a C string variable and then pass that to the function (Because using C string is the proper way to use long groups of text in Arduino).
Modern hardware + compiler optimization render this concern pointless.
Are both of these considered the same then? If not, how are they different? And if you would rather point me to a resource that explains it, that would be awesome.
Doesn't cause an error / warning because it's perfectly acceptable to initialize an char array with a c-string literal. When passed to the print function, myText is treated as a char * which is (presumably) what it's looking for.
Doesn't cause an error / warning because it's perfectly acceptable to initialize an char array with a c-string literal. When passed to the print function, myText is treated as a char * which is (presumably) what it's looking for.
C:\Users\GFV\AppData\Local\Temp\arduino_modified_sketch_328401\sketch_jan28b.ino: In function 'void setup()':
C:\Users\GFV\AppData\Local\Temp\arduino_modified_sketch_328401\sketch_jan28b.ino:6:17: warning: invalid conversion from 'const char*' to 'char*' [-fpermissive]
 myPrint(myText);
        ^