Arduino build error

error: cannot convert 'String' to 'const char*'

An Arduino String is an object. Functions from the C library, such as strlen, strcmp and atoi, want a plain character array, a const char*.

  • Build error
  • 2 common causes
  • Seen 487 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino: In function 'void setup()':
sketch.ino:5:22: error: cannot convert 'String' to 'const char*'
    5 |   int n = strlen(name);
exit status 1

The two are different types, so the compiler will not pass one for the other. A String can hand over its characters with c_str().

Mokxi's compiler, clang, words it as no viable conversion from String to const char *.

Causes

The 2 usual causes with the fix

1A String is passed to a C string function

strlen, strcmp, strcpy and atoi take a const char*. Call c_str() on the String to get one, or use the String methods such as length().

Broken
void setup() {
  Serial.begin(9600);
  String name = "Mokxi";
  int n = strlen(name);
  Serial.println(n);
}

void loop() {
}
Fixed
void setup() {
  Serial.begin(9600);
  String name = "Mokxi";
  int n = strlen(name.c_str());
  Serial.println(n);
}

void loop() {
}

Mokxi’s build tab says: strlen takes C text

Try this fix in Mokxi

2A String is passed to your own function that wants const char*

A function declared with const char* will not take a String. Change the call, or write the function to take a String.

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

void setup() {
  Serial.begin(9600);
  String message = "hello";
  show(message);
}

void loop() {
}
Fixed
void show(const char* text) {
  Serial.println(text);
}

void setup() {
  Serial.begin(9600);
  String message = "hello";
  show(message.c_str());
}

void loop() {
}

Mokxi’s build tab says: show takes C text

Try this fix in Mokxi

FAQ

Questions and answers

What does "cannot convert 'String' to 'const char*'" mean?

A function that wants a plain C string was given an Arduino String object. Call c_str() on the String to pass its characters.

What is the difference between String and char arrays?

A String is an object with methods such as length() and concat(). A char array is a row of bytes that ends in zero. C library functions only know the second.

Should I use String at all on an Uno?

Short, fixed strings are fine. A sketch that joins many Strings in loop() can fragment the 2 KB of memory, so plain char arrays are safer there.