I have a struct that stores some data as a void pointer and a integer like this:
struct container
{
int size;
void* data;
};
Now I have a second struct:
struct date
{
int day;
int year;
};
And I have function that initialized such a container and returns it:
struct container get_container()
{
struct container d = { .data= {(struct date){.day = 1, .year = 2}, (struct date){.day = 4, .year = 5}}, .size = 2};
return d;
}
But when I compile the code the compiler complains that types mismatch:
test.c:19:34: error: initializing 'void *' with an expression of incompatible type 'struct date'
struct container d = { .data= {(struct date){.day = 1, .year = 2}, (struct date){.day = 4, .year = 5}}, .size = 2};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Even if I a void* cast to the init it doesn't work. Is there any way to do this, without using malloc?