#define LENGTH 6
typedef char data_t[LENGTH];
struct foo {
const data_t data;
...
}
...
void bar(data_t data) {
printf("%.6s\n", data);
struct foo myfoo = {*data};
printf("%.6s\n", foo.data);
}
I'm trying to have this struct which holds directly the data I'm interested in, sizeof(foo) == 6+the rest, not sizeof(foo) == sizeof(void*)+the rest. However I can't find a way to initialize a struct of type foo with a data_t. I think maybe I could remove the const modifier from the field and use memcpy but I like the extra safety and clarity.
I don't get any compile errors but when I run the code I get
123456
1??
so the copy didn't work properly I think.
This is for an arduino (or similar device) so I'm trying to keep it to very portable code.
Is it just not possible ?
EDIT: removing the const modifier on the data_t field doesn't seem to help.
data_t datais equivalent tochar *data, and there isn't an easy way around that that I can think of at an hour and a half after bed-time. C doesn't support direct array assignment, of course. Can you pass astruct foo *tobar? Then it's easy:struct foo foo = *data;.typedef char data_t[LENGTH];? Andstruct foo = {*data};doesn't look valid to me...struct foo f {.data = {data[0], data[1], data[2], data[3], data[4], data[5]} }.datais C99, so that's pretty portable. Length=6 is the reason why I didn't care to write this in an answer.