I read through this(How to initialize a unsigned char array?), but it doesn't quite answer my question.
I know I can create an array of strings like this:
const char *str[] =
{
"first",
"second",
"third",
"fourth"
};
and if I want to write() these I can use: write(fd, str[3], sizeof(str[3]));
But what if I need an array of unsigned chars of variable length? I tried this:
const unsigned char *cmd[] =
{
{0xfe, 0x58},
{0xfe, 0x51},
{0xfe, 0x7c, 0x01, 0x02, 0x00, 0x23},
{0xfe, 0x3d, 0x02, 0x0f}
};
and I get gcc compile warnings such as * "braces around scalar initializer" * "initialization makes pointer from integer without cast"
write(fd, str[3], strlen(str[3]));.strlennotsizeof.sizeofon a pointer gives you the size of the pointer, not the size of the data it is pointing to.stris an array of pointers, not of variable-length arrays. It is OK to initialize a pointer with a string literal. There are no array literals and you cannot initialize pointers with braced lists.