Warning message on Build of project (ISO C++ forbids converting a string constant to 'char*') - looking for explanation of it's meaning?

Firstly, thanks for all the help previously it’s massively enabled me to understand and make progress with my projects.

The code below (for me as a novice) started out as an alternative idea instead of using 8 round GC9A01 displays by using the example in TFT_eSPI/examples/320x240/TFT_Meters as a template. The basic display works giving me confidence to progress to the next step by using sensing devices (looking at using Bluetooth) to provide real world measurements.

But each time I make a change (for example add a comment) when I build the code I get the following warnings and I’d like to know is there an alternative coding method as the part of the code exists in the the original meter code.

src/main.cpp: In function 'void setup()':
src/main.cpp:133:27: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Bar",  0, 80);
                           ^
src/main.cpp:134:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Bar",  1 * d, 80);
                               ^
src/main.cpp:135:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Bar",  2 * d, 80);
                               ^
src/main.cpp:136:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Bar",  3 * d, 80);
                               ^
src/main.cpp:137:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("`C",   4 * d, 80);
                               ^
src/main.cpp:138:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Bar",  5 * d, 80);
                               ^
src/main.cpp:139:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("Volt", 6 * d, 80);
                               ^
src/main.cpp:140:31: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   plotLinear("ANY",  7 * d, 80);
                               ^
Linking .pio\build\esp32doit-devkit-v1\firmware.elf
Retrieving maximum program size .pio\build\esp32doit-devkit-v1\firmware.elf
Checking size .pio\build\esp32doit-devkit-v1\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.0% (used 22840 bytes from 327680 bytes)
Flash: [==        ]  23.2% (used 303497 bytes from 1310720 bytes)
Building .pio\build\esp32doit-devkit-v1\firmware.bin
esptool.py v4.9.0
Creating esp32 image...
Merged 2 ELF sections
Successfully created esp32 image.

Here is the code example (modified) I’m using. Please feel free to use.

/*
  Example animated analogue meters using a ILI9341 TFT LCD screen

  Needs Font 2 (also Font 4 if using large scale label)

  Make sure all the display driver and pin connections are correct by
  editing the User_Setup.h file in the TFT_eSPI library folder.

  #########################################################################
  ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
  #########################################################################

  Modified: 25/06/2026
  Verion: Basic Display Configuration
*/


#include <Arduino.h>
#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

#define TFT_GREY 0x5AEB
////#define LOOP_PERIOD 35 // Display updates every 35 ms
#define LOOP_PERIOD 20 // Display updates every 20 ms

uint32_t updateTime = 0; // Time for next update in millis() function

int value[8] = {0, 0, 0, 0, 0, 0,0 ,0}; //Array to hold 
int old_value[8] = { -1, -1, -1, -1, -1, -1,-1, -1};
int d = 0;

// #########################################################################
//  Draw a linear meter on the screen
// #########################################################################

void plotLinear(char *label, int x, int y)
{
  int w = 36;
  
  tft.drawRect(x, y, w, 155, TFT_GREY);
  tft.fillRect(x + 2, y + 19, w - 3, 155 - 38, TFT_WHITE);
  tft.setTextColor(TFT_CYAN, TFT_BLACK);
  tft.drawCentreString(label, x + w / 2, y + 2, 2);

  for (int i = 0; i < 110; i += 10)
  {
    tft.drawFastHLine(x + 20, y + 27 + i, 6, TFT_BLACK);
  }
  for (int i = 0; i < 110; i += 50)
  {
    tft.drawFastHLine(x + 20, y + 27 + i, 9, TFT_BLACK);
  }

  tft.fillTriangle(x + 3, y + 127, x + 3 + 16, y + 127, x + 3, y + 127 - 5, TFT_RED);
  tft.fillTriangle(x + 3, y + 127, x + 3 + 16, y + 127, x + 3, y + 127 + 5, TFT_RED);

  tft.drawCentreString("---", x + w / 2, y + 155 - 18, 2);
}

// #########################################################################
//  Adjust 8 (was 6) linear meter pointer positions
// #########################################################################
void plotPointer(void)
{ 
  byte pw = 16;
  ////int dy = 187;
  int dy = 106; //Change this to align text  
  int xTxt = 165; //Change this to align text
  int yTxt = 10; //Change this to align text
  
  tft.setTextColor(TFT_CYAN, TFT_BLACK);
  tft.setRotation(0);
  tft.drawString("Left Front",   xTxt, yTxt,2);
  tft.drawString("Left Rear",    xTxt,yTxt+40,2);
  tft.drawString("Right Front",  xTxt,yTxt+80,2);
  tft.drawString("Right Rear",   xTxt,yTxt+120,2);
  tft.drawString("Coolant",      xTxt,yTxt+160,2);
  tft.drawString("Oil ",         xTxt,yTxt+200,2);
  tft.drawString("Battery",      xTxt,yTxt+240,2);
  tft.drawString("OtherMeas't",  xTxt,yTxt+280,2);
  tft.setRotation(1);  

  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  // Move the 8 pointers one pixel towards new value
  for (int i = 0; i < 8; i++)
  { // Turn Floats (numbers) into Strings (characters)
    // dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
    char buf[8]; dtostrf(value[i], 4, 0, buf);

    //drawRightString(const char *string, int32_t dX, int32_t poY, uint8_t font)
    /*
     const char* string: String to be displayed as array of char, null terminated.
     ìnt32_t dX: end x coord.
     ìnt32_t poY: y coord.
     uìnt32_t font: Font to be used.
    */
    tft.drawRightString(buf, i * 40 + 36 - 5, 106 - 27 + 155 - 18, 2);

    int dx = 3 + 40 * i;
    if (value[i] < 0) value[i] = 0;     // Limit value to emulate needle lower end stop.
    if (value[i] > 100) value[i] = 100; // Limit value to emulate needle upper end stop.

    while (!(value[i] == old_value[i])) {
      ////dy = 187 + 100 - old_value[i]; Original Example Code
      dy = 106 + 100 - old_value[i];
      if (old_value[i] > value[i])
      {
        tft.drawLine(dx, dy - 5, dx + pw, dy, TFT_WHITE);
        old_value[i]--;
        tft.drawLine(dx, dy + 6, dx + pw, dy + 1, TFT_RED);
      }
      else
      {
        tft.drawLine(dx, dy + 5, dx + pw, dy, TFT_WHITE);
        old_value[i]++;
        tft.drawLine(dx, dy - 6, dx + pw, dy - 1, TFT_RED);
      }
    }
  }
}

void setup(void) {
  ////Serial.begin(115200); // For debug
  tft.init();
  tft.setRotation(1);  
  tft.fillScreen(TFT_BLACK);

  // Draw 8 linear meters, original exaple code draws 6.
  // Declare a typedef uint8_t value of 40.
  byte d = 40;
  //Position each meters quantity or dimension.
  plotLinear("Bar",  0, 80);
  plotLinear("Bar",  1 * d, 80);
  plotLinear("Bar",  2 * d, 80);
  plotLinear("Bar",  3 * d, 80);
  plotLinear("`C",   4 * d, 80);
  plotLinear("Bar",  5 * d, 80);
  plotLinear("Volt", 6 * d, 80);
  plotLinear("ANY",  7 * d, 80);

  updateTime = millis(); // Next update time
}

void loop() {
  int angle =0;
  int period = 60;

  if (updateTime <= millis()) {
    updateTime = millis() + LOOP_PERIOD;

    // Create a Sine wave for testing
    d += 2; if (d >= 360) d = 0;    
    value[0] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[1] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[2] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[3] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[4] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[5] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[6] = 50 + 50 * sin((d + angle) * 0.0174532925);
    angle = angle+period;
    value[7] = 50 + 50 * sin((d + angle) * 0.0174532925);
    if (angle >= 420) angle = 0; // 8 * 60

    plotPointer();
  }
}

Hope this makes sense, thanks in advance.

Degs

Change char *label to const char *label
You don’t pass a buffer (char*) as parameter but a const char*

1 Like

Hi Boris, thanks I see it now…..

int16_t TFT_eSPI::drawString(const char *string, int32_t x, int32_t y, uint8_t font)


:+1: