Home > Net >  Copy temporary buffer to out buffer
Copy temporary buffer to out buffer

Time:11-11

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)

CodePudding user response:

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])
  •  Tags:  
  • c
  • Related