warning: ISO C++ forbids converting a string constant to 'char*'

Hi,

Not sure if I can ignore following message I get while compiling,

warning: ISO C++ forbids converting a string constant to ‘char*’

but I don’t like to see warnings nor errors.

Here are the warning messages:

Documents/Arduino/sketch_DewCrtl48_20160123_MultiDew/sketch_DewCrtl48_20160123_MultiDew.ino: In function ‘void powerup()’:
Documents/Arduino/sketch_DewCrtl48_20160123_MultiDew/sketch_DewCrtl48_20160123_MultiDew.ino:517:24: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] char* strCurrentVolt = “00.00”;
^
Documents/Arduino/sketch_DewCrtl48_20160123_MultiDew/sketch_DewCrtl48_20160123_MultiDew.ino:518:21: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] char* strHighVolt = “00.00”;
^
Documents/Arduino/sketch_DewCrtl48_20160123_MultiDew/sketch_DewCrtl48_20160123_MultiDew.ino:519:20: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] char* strLowVolt = “99.99”;
^

Here is the portion of the code:

resetLCD();
char* strCurrentVolt = "00.00";
char* strHighVolt = "00.00";
char* strLowVolt = "99.99";
float fHighVolt, fLowVolt;

strHighVolt[0] = EEPROM.read(30);
strHighVolt[1] = EEPROM.read(31);
strHighVolt[2] = EEPROM.read(32);
strHighVolt[3] = EEPROM.read(33);
strHighVolt[4] = EEPROM.read(34);
strHighVolt[5] = EEPROM.read(35);
fHighVolt = atof(strHighVolt);

strLowVolt[0] = EEPROM.read(40);
strLowVolt[1] = EEPROM.read(41);
strLowVolt[2] = EEPROM.read(42);
strLowVolt[3] = EEPROM.read(43);
strLowVolt[4] = EEPROM.read(44);
strLowVolt[5] = EEPROM.read(45);
fLowVolt = atof(strLowVolt);

lcd.setCursor(0,0);
lcd.print("High V=");
lcd.print(fHighVolt);
lcd.setCursor(0,1);
lcd.print(" Low V=");
lcd.print(fLowVolt);

I am also attaching reset of the code.

Any help is appreciated.

sketch_DewCrtl48_20160123_MultiDew-2.zip (157 KB)

char* strCurrentVolt = "00.00"; char* strHighVolt = "00.00"; char* strLowVolt = "99.99";

use ?

char* strCurrentVolt = '00.00'; char* strHighVolt = '00.00'; char* strLowVolt = '99.99';

shrug

Depends how you intend to use those strings. I'm not downloading and unpacking a 157 KB zip file to find out. But, if they are constants, use:

const char * const strCurrentVolt = "00.00";

or

const char strCurrentVolt[] = "00.00";

Idahowalker: char* strCurrentVolt = "00.00"; char* strHighVolt = "00.00"; char* strLowVolt = "99.99";

use ?

char* strCurrentVolt = '00.00'; char* strHighVolt = '00.00'; char* strLowVolt = '99.99';

shrug

Nope.

Use ' instead of "

I agree with gfvalvo.

Nope.

Unless 00.00 is a single character, say a single key on a keyboard, double quotes should be used and NOT single quotes.

I tried with single quote and I get the following message:

warning: invalid conversion from 'int' to 'char*' [-fpermissive] char* strCurrentVolt = '00.00';

If I use NO quotes and just 00.00, I get this:

cannot convert 'double' to 'char*' in initialization

If I use what gfvalvo suggested:

assignment of read-only location '(const char)strHighVolt'

anwenet: If I use what gfvalvo suggested:

assignment of read-only location '(const char)strHighVolt'

That means you're attempting to change its value somewhere, which is why I prefaced my suggestion with "if they are constants".

I don't think that they are constants. Assigning 00.00 to a variable is just silly unless this is a way to reserve storage.

Here is power section

// ********************powerup function********************
void powerup(void)
{
 resetLCD();

if (temperatureC) {
 displayUpdate("Temperature", "Celsius", "Display = Celsius");
 }
else{
 displayUpdate("Temperature", "Fahrenheit", "Display = Fahrenheit");
 }
// Display the stored maximum / minium values
resetLCD();
lcd.print("Max Temp=");
sensorhigh = EEPROM.read(15);
displaySensorValue(sensorhigh);
if(EEPROM.read(10) > 0) {
 lcd.print(" LowV");
}
lcd.setCursor(0,1);
sensorhigh = EEPROM.read(16);
lcd.print("Hum=");
displaySensorValue(sensorhigh);
sensorhigh = EEPROM.read(17);
lcd.print(" Dew=");
displaySensorValue(sensorhigh);
delay(displayDelay2);

resetLCD();
lcd.print("Min Temp=");
sensorhigh = EEPROM.read(25);
displaySensorValue(sensorhigh);
if(EEPROM.read(10) > 0) {
 lcd.print(" LowV");
}
lcd.setCursor(0,1);
lcd.print("Hum=");
sensorhigh = EEPROM.read(26);
displaySensorValue(sensorhigh);
lcd.print(" Dew=");
sensorhigh = EEPROM.read(27);
displaySensorValue(sensorhigh);
delay(displayDelay2);

resetLCD();
lcd.print("Max S1=");
sensorhigh = EEPROM.read(11);
displaySensorValue(sensorhigh);

lcd.print(" S2=");
sensorhigh = EEPROM.read(12);
displaySensorValue(sensorhigh);

lcd.setCursor(3,1);
lcd.print(" S3=");
sensorhigh = EEPROM.read(13);
displaySensorValue(sensorhigh);

lcd.print(" S4=");
sensorhigh = EEPROM.read(14);
displaySensorValue(sensorhigh);
delay(displayDelay2);

resetLCD();
lcd.print("Min S1=");
sensorhigh = EEPROM.read(21);
displaySensorValue(sensorhigh);

lcd.print(" S2=");
sensorhigh = EEPROM.read(22);
displaySensorValue(sensorhigh);

lcd.setCursor(3,1);
lcd.print(" S3=");
sensorhigh = EEPROM.read(23);
displaySensorValue(sensorhigh);

lcd.print(" S4=");
sensorhigh = EEPROM.read(24);
displaySensorValue(sensorhigh);

delay(displayDelay2);

resetLCD();
char* strCurrentVolt = "00.00";
char* strHighVolt = "00.00";
char* strLowVolt = "99.99";
//const char* const strCurrentVolt = "00.00";
//const char* const strHighVolt = "00.00";
//const char* const strLowVolt = "99.99";
//char* strCurrentVolt = 00.00;
//char* strHighVolt = 00.00;
//char* strLowVolt = 99.99;
float fHighVolt, fLowVolt;

strHighVolt[0] = EEPROM.read(30);
strHighVolt[1] = EEPROM.read(31);
strHighVolt[2] = EEPROM.read(32);
strHighVolt[3] = EEPROM.read(33);
strHighVolt[4] = EEPROM.read(34);
strHighVolt[5] = EEPROM.read(35);
fHighVolt = atof(strHighVolt);

strLowVolt[0] = EEPROM.read(40);
strLowVolt[1] = EEPROM.read(41);
strLowVolt[2] = EEPROM.read(42);
strLowVolt[3] = EEPROM.read(43);
strLowVolt[4] = EEPROM.read(44);
strLowVolt[5] = EEPROM.read(45);
fLowVolt = atof(strLowVolt);

lcd.setCursor(0,0);
lcd.print("High V=");
lcd.print(fHighVolt);
lcd.setCursor(0,1);
lcd.print(" Low V=");
lcd.print(fLowVolt);

delay(displayDelay2);

resetLCD();
if ((ts0) <= 0){
 EEPROM.write(0, ((EEPROM.read(0)+1)));
 // If no sensors present twice in a row, then clear the stored max values.
 if (EEPROM.read(0) > 1) { 
 clearAppEEPROM();
 }
}  
 // If an new version installed, then clear the stored max values.  
if (EEPROM.read(1) != (ver)) {
 clearAppEEPROM();
}

sessionNumberMSD = (EEPROM.read(98));
sessionNumberLSD = (EEPROM.read(99));
sessionNumber=((sessionNumberMSD * 255) + sessionNumberLSD);
sessionNumber=sessionNumber + 12;
if (sessionNumber > 4092){
sessionNumber = 1;
}
sessionNumberMSD = sessionNumber / 255;
sessionNumberLSD = sessionNumber % 255;
EEPROM.write(98, sessionNumberMSD);
EEPROM.write(99, sessionNumberLSD);

// Found a Dallas one wire sensor, so reset the counter for clearing EEPROM.
if ((ts0) > 0) {
 EEPROM.write(0, (0)); 
} 
 displayUpdate((whoami), "**** 4 Port ****", (whoami)); 

// Ver 4.3- Temperature is being displayed in Celsius format, so display the version date in international format. 
if (temperatureC)
 ver2 = (ver2b += ver2a += ver2c);
// Ver 4.3 - Temperature is being displayed in Fahrenheit format, so display the version date in US format.  
else 
 ver2 = (ver2a += ver2b += ver2c);

 displayUpdate((ver1), (ver2), ((ver1)+(ver2)));   
 if (noRTC == 0) { // Ver 4.3 - If no RTC present do not attempt to display the date and time.
   resetLCD();
   displayCurrentDateTime();
 }  
 resetLCD();
}
// ****************end of powerup function*****************

There are (multiple) better ways to do it:

void powerup(void) {
  char tempBuffer[8];  // I always like to make bufferes a little bigger than needed
  //
  //
  //
  tempBuffer[0] = EEPROM.read(30);
  tempBuffer[1] = EEPROM.read(31);
  tempBuffer[2] = EEPROM.read(32);
  tempBuffer[3] = EEPROM.read(33);
  tempBuffer[4] = EEPROM.read(34);
  tempBuffer[5] = EEPROM.read(35);
  tempBuffer[6] = '\0';  // just to play it safe
  fHighVolt = atof(tempBuffer);

  tempBuffer[0] = EEPROM.read(40);
  tempBuffer[1] = EEPROM.read(41);
  tempBuffer[2] = EEPROM.read(42);
  tempBuffer[3] = EEPROM.read(43);
  tempBuffer[4] = EEPROM.read(44);
  tempBuffer[5] = EEPROM.read(45);
  tempBuffer[6] = '\0';  // just to play it safe
  fLowVolt = atof(tempBuffer);

  lcd.setCursor(0, 0);
  lcd.print("High V=");
  lcd.print(fHighVolt);
  lcd.setCursor(0, 1);
  lcd.print(" Low V=");
  lcd.print(fLowVolt);
  //
  //
  //
}

OR:

void powerup(void) {
  char tempBuffer[8];  // I always like to make bufferes a little bigger than needed
  //
  //
  //
  tempBuffer[0] = EEPROM.read(30);
  tempBuffer[1] = EEPROM.read(31);
  tempBuffer[2] = EEPROM.read(32);
  tempBuffer[3] = EEPROM.read(33);
  tempBuffer[4] = EEPROM.read(34);
  tempBuffer[5] = EEPROM.read(35);
  tempBuffer[6] = '\0';  // just to play it safe
  lcd.setCursor(0, 0);
  lcd.print("High V= ");
  lcd.print(tempBuffer);

  tempBuffer[0] = EEPROM.read(40);
  tempBuffer[1] = EEPROM.read(41);
  tempBuffer[2] = EEPROM.read(42);
  tempBuffer[3] = EEPROM.read(43);
  tempBuffer[4] = EEPROM.read(44);
  tempBuffer[5] = EEPROM.read(45);
  tempBuffer[6] = '\0';  // just to play it safe
  lcd.setCursor(0, 1);
  lcd.print(" Low V=");
  lcd.print(tempBuffer);
  //
  //
  //
}

Or, if the values were originally floats before being stored, just save them in EEPROM as (binary) floats. Then, extract them as such. That only requires 4 bytes each.

EDIT: Also, in the first two examples above, the unnecessary repetition can be avoided by using 'for' loops.

Idahowalker: char* strCurrentVolt = "00.00"; char* strHighVolt = "00.00"; char* strLowVolt = "99.99";

use ?

char* strCurrentVolt = '00.00'; char* strHighVolt = '00.00'; char* strLowVolt = '99.99';

shrug

Um no. If you don't know what you're doing then please refrain from giving advice until you do. At least on things you obviously have no clue about.

Single quotes are for single characters. Not strings.

Delta_G: Um no. If you don't know what you're doing then please refrain from giving advice until you do. At least on things you obviously have no clue about.

Single quotes are for single characters. Not strings.

Single quotes are for characters. Not necessarily single characters. Please refrain, blah blah blah.

AWOL: Single quotes are for characters. Not necessarily single characters. Please refrain, blah blah blah.

OK, educate me here then. Where might I need single quotes with multiple characters in an Arduino context? I know you can get things to compile that way and "work", but where would that be useful?

Up to two character tags, where you can't afford/don't want the overhead (+ 50 to 100%!) of a terminating null for a string.

(Sorry,I didn't realise pointing-out wrongness had to be restricted to Arduinoland)

Wait a second.. Are you saying that the only difference between 'aString' and "aString" is that the one in double quotes has a terminating '\0'?

-jim lee

void setup() {
  Serial.begin(115200);
  delay(250);

  for(int i=0; i<3; i++)Serial.println();

  int x = 'AB';
  Serial.print(x, HEX);

}

void loop() {
  

}

Dutifully prints “4142”. I guess you’re right. It’s not just single characters, but characters in general. Still doesn’t make that bad answer right. But you learn something new every day.

jimLee:
Wait a second… Are you saying that the only difference between ‘aString’ and “aString” is that the one in double quotes has a terminating ‘\0’?

-jim lee

No, not at all.

I’m saying that ‘It’ will fit in a sixteen bit integer, but “It” will not.

Looks like the Single Quote method is effected by Endianness:

void setup() {
  uint16_t string1 = 'AB';
  char string2[] = "AB";
  uint8_t *ptr;
  Serial.begin(115200);
  delay(1000);

  Serial.println("Single Quotes:");
  ptr = (uint8_t *)(&string1);
  for (uint8_t i = 0; i < 2; i++) {
    Serial.print(*ptr, HEX);
    Serial.print("(");
    Serial.print((char)(*ptr++));
    Serial.print(") ");
  }

  Serial.println();
  Serial.println();
  Serial.println("Double Quotes:");
  ptr = (uint8_t *)(string2);
  for (uint8_t i = 0; i < 2; i++) {
    Serial.print(*ptr, HEX);
    Serial.print("(");
    Serial.print((char)(*ptr++));
    Serial.print(") ");
  }
}

void loop() {}

Output:

Single Quotes:
42(B) 41(A) 

Double Quotes:
41(A) 42(B)