?Send a temp String to a Function OR storing a C string, then send to function?

Hey!

If I get anything wrong, let me know. :)

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?

myGLCD.print("Hello World!", 10, 150);

-or-

const char myText[13] = "Hello World!";

myGLCD.print(myText, 10, 150);

The reason I also ask is because the compiler throws this error for the first example: ISO C++ forbids converting a string constant to 'char*'

My understanding of String and C string comes from this - https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

But it doesn't cover what to do when you just need to send a simple text to a function. .

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).

Such as this:

const char myText[13] = "Hello World!";

myGLCD.print(myText, 10, 150);

(Which the compiler doesn't whine at all about.)

But which one uses less memory and less computing power?

.

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.

const char myText[13] = "Hello World!";

myGLCD.print(myText, 10, 150);

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.

Ok. Thank you. I'm trying to understand that.

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.

(I changed it a little bit.)

myGLCD.print("Hello World!", 10, 150);

// vs.

const char *myText = "Hello World!";

myGLCD.print(myText, 10, 150);

.

Actually,

const char myText[13] = "Hello World!";
myGLCD.print(myText, 10, 150);

is the same as:

const char* const myText = "Hello World!";
myGLCD.print(myText, 10, 150);

which will generate the same error/warning as:

myGLCD.print("Hello World!", 10, 150);

or

const char* myText = "Hello World!";
myGLCD.print(myText, 10, 150);

Working code is:

char myText[13] = "Hello World!";
myGLCD.print(myText, 10, 150);

Ok. From what you said, that would make sense that it should cause a Warning/Error.

But it doesn’t throw a warning. Would you know why?

mafokken: Ok. From what you said, that would make sense that it should cause a Warning/Error.

But it doesn't throw a warning. Would you know why?

No idea!.

char myText[13] = "Hello World!";
myGLCD.print(myText, 10, 150);

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.

gfvalvo: char myText[13] = "Hello World!"; myGLCD.print(myText, 10, 150);

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.

I think he meant:

const char myText[13] = "Hello World!";
myGLCD.print(myText, 10, 150);

does not cause error/warning.

Right.

I don’t have that library, but:

void myPrint(char *ptr);

const char myText[] = "Hello World!";

void setup() {
  myPrint(myText);
}

void loop() {
}

void myPrint(char *ptr) {
  Serial.println(ptr);
}
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);

                 ^