can't read data from void pointer:
#include <windows.h>
typedef enum {
ADDRESS,
PERSON,
} DataType;
typedef struct {
DataType type;
void* data;
} Data;
Data* create_data(DataType type, void* data);
typedef struct {
char* number;
char* street;
char* city;
char* state;
char* postalCode;
} Address;
typedef struct {
int age;
char* name;
} Person;
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
) {
// WRITE
Address* home = malloc(sizeof(Address));
home->number = "123";
home->street = "Main";
home->city = "New York";
home->state = "NY";
home->postalCode = "10001";
Data* addressdata = create_data(ADDRESS, &home);
// READ
char* addressstreet = ((Address*)addressdata->data)->street;
}
Data* create_data(DataType type, void* data)
{
Data* d = (Data*)malloc(sizeof(Data));
d->type = type;
d->data = data;
return d;
}
After reading your question, the first thing popped in my head is that how can you dereference a void pointer since it has no object type.
There's parameter
mismatch in
Data* create_data(DataType type, void* data);
&Data* addressdata = create_data(ADDRESS, &home);
and instead of sending address of home
, i.e. create_data(ADDRESS, &home);
send create_data(ADDRESS, home);