Home > Back-end >  Issue building a char from sting and variables
Issue building a char from sting and variables

Time:12-29

I'm trying to build a string or character to send to a function.

error:

In member function 'void RTSSHOW::RTS_Init()':
Marlin/src/lcd/e3v2/creality/LCD_RTS.cpp:383:37: warning: '%f' directive writing between 3 and 317 bytes into a region of size 0 [-Wformat-overflow=]
  383 |         sprintf_P((char*)mstr, PSTR("%s %f"), mstr, z_values[x][y] * 1000);

Short code summary:

auto mstr = (char*)""; // here is the string I'm trying to build

sprintf_P((char*)mstr, PSTR("%s %f"), mstr, z_values[x][y] * 1000);

sprintf_P((char*)mstr, PSTR("%s %s"), mstr, "\n");

RTS_SndData((char*)mstr, AUTO_BED_LEVEL_MESH_VP);

here is my code pastebin: https://pastebin.com/xrk2GJsh

I'm not fluent in C and building a string is not the same as in PHP which I am used to.

I've tried various string and char declarations, but cannot seem to get it.

CodePudding user response:

Some guess work here, but I would try something like this

#include <sstream>
#include <string>

std::ostringstream mstr;
mstr << z_values[x][y] * 1000 << '\n';
RTS_SndData(mstr.str().c_str(), AUTO_BED_LEVEL_MESH_VP);

If that's not quite the correct string, no doubt you can tinker with the code.

Somewhat less flexible, but maybe less heavy is this

#include <string>

std::string mstr = std::to_string(z_values[x][y] * 1000)   "\n";
RTS_SndData(mstr.c_str(), AUTO_BED_LEVEL_MESH_VP);
  • Related