My C-API takes an array of uint8_t's as config parameter. I'm arriving at its doorsteps with a const char*. How do I now copy the chars over to the uint8_t array in the most unproblematic way? Here's what I've got (contrived ofc):
#include <cstdint>
#include <cstring>
#include <cstdio>
struct config
{
uint8_t ssid_[32];
};
auto set_ssid(const char* ssid) {
// I know this fn has no sideeffects but assume for demonstration
// purposes that cfg is only initialized here
config cfg;
std::strncpy(static_cast<char*>(&cfg.ssid_), ssid, 32);
}
int main()
{
set_ssid("ComeOver");
}
But this doesn't work as none of the pointer is of void* type:
<source>:14:18: error: invalid 'static_cast' from type 'uint8_t (*)[32]' {aka 'unsigned char (*)[32]'} to type 'char*'
14 | std::strncpy(static_cast<char*>(&cfg.ssid_), ssid, 32);
|
Is it safe to reinterpret_cast
here?