Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copy temporary buffer to out buffer

In C++, how do I copy a temporary buffer (buffer) to out buffer (outBuffer) using strncpy?

void writeSensorStatus(SensorStatus& data, char* outBuffer[256])
{
  // create temporary buffer
  char buffer[256];
  const size_t capacity = JSON_OBJECT_SIZE(3);
  StaticJsonDocument<capacity> doc;

  
  serializeJson(data, buffer);
  strncpy(outBuffer, buffer, sizeof(outBuffer)); // Problem is here
}

I get the following error, on the lien trying to copy: cannot convert 'char**' to 'char*'

What I’m trying to do here is to retrieve the new values added to buffer outside the method. (Like a return)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

void writeSensorStatus(SensorStatus& data, char* outBuffer[256])

When char* outBuffer[256] is passed as a function parameter, it decays to a pointer to a pointer to char, not a string.

Change this to:

void writeSensorStatus(SensorStatus& data, char* outBuffer)

But this will affect sizeof(outBuffer), so you can use what @paddy suggests, pass by reference:

void writeSensorStatus(SensorStatus& data, char (&outBuffer)[256])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading