Hello,
I am currently trying to outsource some functions to an external C++ file. I have come quite far with it, but unfortunately I am not familiar enough with C++. The file I have created looks like this:
#include <cstdio>
#include "esp_log.h"
static const char* TAG = "converters.ccp";
const char * float_to_char(float input) {
char buffer[64];
int ret = snprintf(buffer, sizeof buffer, "%f", input);
if (ret < 0) {
// error while converting
ESP_LOGE(TAG, "Error while converting float to char [%.1f%%]");
return "";
}
if (ret >= sizeof buffer) {
// Result was truncated - resize the buffer and retry.
ESP_LOGW(TAG, "Result was truncated while converting float to char [%.1f%%]");
}
return buffer;
}
I get the following error message:
src/converters.cpp: In function 'const char* float_to_char(float)':
src/converters.cpp:7:10: warning: address of local variable 'buffer' returned [-Wreturn-local-addr]
char buffer[64];
^~~~~~
When I change the definition of buffer to “char *” I get the following error message:
src/converters.cpp: In function 'const char* float_to_char(float)':
src/converters.cpp:8:24: error: cannot convert 'char**' to 'char*'
int ret = snprintf(buffer, sizeof buffer, "%f", input);
^~~~~~
In file included from /data/cache/platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/cstdio:42,
from src/converters.cpp:1:
/data/cache/platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/sys-include/stdio.h:266:15: note: initializing argument 1 of 'int snprintf(char*, size_t, const char*, ...)'
int snprintf (char *__restrict, size_t, const char *__restrict, ...)
^~~~~~
src/converters.cpp:20:12: error: cannot convert 'char**' to 'const char*' in return
return buffer;
^~~~~~
What am I doing wrong?