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

Hi all,

I can’t find how to fix this warning. Pointers are not my most favorites :slight_smile:

so I call fanction:

prnt_1arg("this text shall go to serial terminal and argument is equal = %d", integer_variable);

and function look like this:

UnbufferedSerial pc(USBTX, USBRX, 115200);
const char *new_line= "\n\r";
char buf_out[32];

void prnt_1arg(char *buff , int value){
   int newSize = strlen(buff)  + strlen(new_line) + 1;
   char * newBuffer = (char *)malloc(newSize);
   strcpy(newBuffer,buff);
   strcat(newBuffer,new_line); 
   sprintf(buf_out, newBuffer, value);
   pc.write(&buf_out, strlen(buf_out));    //
   memset(buf_out, '\0', sizeof(buf_out));
}

}

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

WhatI need to change to remove warnings ?

Thanks
Michal

Hello Michal,

I still get a warning: ISO C++ forbids converting a string constant to char*. What I need to change to remove warnings ?

  • Replace
prnt_1arg("this text shall go to serial terminal and argument is equal = %d", integer_variable);

with

char msg[] = "this text shall go to serial terminal and argument is equal = %d";
prnt_1arg(msg, integer_variable);
  • or change the prnt_1arg signature as follows:
void prnt_1arg(const char *buff , int value) {
...
1 Like

Thanks a lot.
(const char *buff,…)
works great!

Michal

This is also “best practice” as now you are not lying to the compiler anymore.