Pointer char to char array ESP-NOW programming question

hi
i use from here: https://docs.espressif.com/projects/arduino-esp32/en/latest/api/espnow.html the sample program 2. ESP-NOW Broadcast Slave Example:

Now my question: after receiving data from Master there are this lines:

  // Function to print the received messages from the master
  void onReceive(const uint8_t *data, size_t len, bool broadcast) {
    Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
    Serial.printf("  Message: %s\n", (char *)data);
  }

So this works all well. But how do i get the "(char *)data" into my own char array. I have read that this could be done by the vector functions but didn't get a clue how this should work. Can anyone give me a link or a sample code how i get this data to work with it in the sketch. That would be nice.
hape

const uint8_t buf_len = 50; //adjust to adequate size
char buf[buf_len] = {0};
// Function to print the received messages from the master
  void onReceive(const uint8_t *data, size_t len, bool broadcast) {
    Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
    Serial.printf("  Message: %s\n", (char *)data);
    memset(buf,0,buf_len); //reset the buffer
    memcpy(buf,data,min(len,buf_len-1); //copy data to buffer
  }

This should work.

Ciao, Ale.

I assume you do need it to survive the function call. (if not just use the data pointer)

let's say you have a global buffer

const size_t maxMessageSize = 100;
char message[maxMessageSize]; // remember the need for the trailing null
bool messageAvailable = false;

then if you want to copy the content of data into message you can use memcpy()

// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
  if (len < maxMessageSize) { // or could use the macro min(x,y)
    memcpy ( message, data, len );
    message[len] = '\0';
  } else {
    memcpy ( message, data, maxMessageSize-1 );
    message[maxMessageSize-1] = '\0';
  }
  ...; // you can do stuff here on the message
  messageAvailable = true;  // or later in the loop if you set a flag
}

then in your loop you can do something like

void loop() {
  if (messageAvailable) {
    // deal with the message 
    ...
    messageAvailable = false;
  } 
}

There's no reason to "reset" the buffer.

there is a small one, if data is not null terminated or larger than buf_len-1 - then having 0 everywhere makes sure the c-string is null terminated...

(but I agree it would be faster to just add the trailing null)

So don't do it in your program.

Ciao, Ale.

thx a lot for this really usefull answers i will try it and come back with my experience. I think it will work well so i will write my thoughts and flag it than as resolved.

hi
works nice. On the solution from @ilguargua a get a compile error so i tried the other solution from @J-M-L and this works like a charm. So i'm happy and thx to all helping me :-).
hape

good - have fun!

Yes, I miss a parentesis on last statment...

Ciao, Ale.