title: String handling in C language for Espressif ESP-IDF
body: <p>The following function call is derived from espressif ESP-IDF and will send an mqtt message  (event->data) with length event->data_len).</p>
  
  <p><code>esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, event-&gt;data , event-&gt;data_len, 1, 0);</code></p>
  
  <p>Instead, if event->data I want to extend this character array by some additional characters like "I got [event->data] !" and send this message by the function shown above.</p>
  
  <p>I know, there are many ways how to achieve this, but every solution I found, results in a couple of lines that are ridiculous complicated in my eyes.</p>
  
  <pre><code>    //Buffer to hold event-&gt;data as a string with \0 termination
      char *bufData = calloc(event-&gt;data_len + 1, sizeof(char));
      snprintf(bufData, event-&gt;data_len + 1, "%s", event-&gt;data); // get a zero ended string from event-&gt;data
  
      //Additional Buffer to hold complete String
      int mqttStringBufSize = strlen(bufData) + 30;
      char *mqttStringBuf = calloc(mqttStringBufSize, sizeof(char));
      snprintf(mqttStringBuf, mqttStringBufSize, "I received [%s]", bufData);
  
      esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC,
              mqttStringBuf, strlen(mqttStringBuf), 1, 0);
  
      free(bufData);
      free(mqttStringBuf);
  </code></pre>
  
  <p>So I am looking for a clean solution to this problem.</p>