Calling method with *parameter

I have some code that calls a method that accepts a char *text.

void bar() {
  foo("0123");
  String text = "0123";
  foo(text);
}

void foo(const char *text) {
  // Do stuff
}

It compiles if I pass it a string literal, but if I pass it a string variable it throws a compile time exception:

cannot convert 'String' to 'const char*' for argument '1' to 'void foo(const char*)'
   foo(text);

The error message tells you exactly what is wrong. Don't use a String, use a C style string instead

void setup()
{
  Serial.begin(115200);
  char * message = "0123";
  foo(message);
}

void loop()
{
}

void foo(const char *text)
{
  Serial.println(text);
}

Do not use the "String" class on AVR based boards, it will most likely cause problems at some point. If you have to, use "String.c_str()" to get a "char*" string.

I understood the message, I didn't understand why the string literal worked but the String type didn't. Now I know, String is a different type to "Foo". I thought it was odd that String was capitalised.

Is "Foo" of type char *?

Actually that throws a compile-time exception:

 warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   char * message = "0123";

The foo() function requires a parameter that is a pointer to a C style string (lowercase s) . This could be either a string variable, which is itself a zero terminated array of chars, or a string constant in quotes

  char * message = "from the message array";
  foo(message);
  foo("this is a string constant");

If I make the declaration a const then things are happy.

void setup() {
  const char * message = "0123";
  foo(message);
}

void foo(const char *text) {
  Serial.println(text);
}

I need to be able to pass it a variable though, do I need to put my variable into a const before passing it to the method? e.g.

char * foo = "1";
const char * bar = foo;

No. Try this:

void setup()
{
  Serial.begin(115200);
  char message[] = "0123";  //note brackets
  foo(message);
}

void loop()
{
}

void foo(char *text)
{
  Serial.println(text);
}

The const tells the compiler that the foo function will not alter what the char* points to. You can pass a char* that is not const without any problems, you just cannot pass a const char* to a function that takes a char* because the function is not prohibited from altering it.

This is your problem:

The 'char * message' part means that 'message' is a pointer to a char and the char (or chars) being pointed to can be modified via the pointer.

The compiler doesn't like that since you're pointing 'message' at a string literal that should never be modified.

As you discovered this fixes it:

const char * message = "0123";

Now you can't modify the literal via the pointer.

or use:

char message[] = "0123";

or

const char message[] = "0123";

All of the above can be used in a call to your function:

void foo(const char *text) {
  // Do stuff
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.