I try to fill the parent structure with a memcpy. I work on embedded system (with Contiki OS). So I try to avoid using a malloc because I read that it is not recommended.
I test the code on my computer and I get the error Segmentation fault (core dumped).
On Contiki there are no errors but the values after the fill call are not the expected ones.
I have no idea why this error occurs and I don't know another way to do this.
Here is an example of the code that looks like what I'm trying to do:
Code:
typedef struct child{
uint8_t age;
uint16_t size;
}child_t;
typedef struct parent{
child_t *child1;
child_t *child2;
bool has_child;
}parent_t;
void fill(parent_t *dest){
parent_t parent;
child_t child;
uint8_t age=24;
uint16_t size=180;
child.age=age;
child.size=size;
printf("h2\n");
memcpy(parent.child1, &child, sizeof(child_t));//Segmentation fault (core dumped)
printf("h3\n");
memcpy(dest, &parent, sizeof(parent_t);
}
int main(int argc, char *argv[]){
parent_t parent;
fill(&parent);
return 0;
}