Hi!
It's a common pattern to define "length-prefixed" string literals using compond literals in this way:
struct str {
size_t len;
const char* s;
};
#define str(literal) ((struct str){.len=sizeof(literal)-1, .s=literal})
const struct str hello = str("HELLO");
However, doing so will make the string length be stored in the stack while the literal will continue to be stored in the .rodata section of the program. I was wondering if there's a way to make the string literal's length be prefixed along with the string itself in the .rodata section of the program and be left with:
struct str {
size_t len;
const char s[];
};
// some magic
const struct str* hello = // ???