I'm trying to create array of void pointers inside my struct to see if that is possible. I want to be in charge of the memory allocation and to be able to update the value for each array by index. The value data type is not specified as i want to accept any data type.
This is what i did:
typedef struct {
void ** value;
} bucket;
void updateValue(bucket * data, index, void * value)
{
if(data->value[index] == NULL)
{
data->value[index] = (void*)calloc(1, sizeof(void*));
}
data->value[index] = value;
}
bucket * clients = calloc(1, sizeof(bucket));
clients->value = (void **)calloc(3, sizeof(void*));
clients->value[0] = NULL;
clients->value[1] = NULL;
clients->value[2] = NULL;
updateValue(clients, 0, (void*) (int)124);
printf("Client0 Value: value: %d\n", (int)&clients->value[0]);
The code compile, but does not output 124 as value. I don't know what is wrong. Can someone please help me to correct it and explain what wrong so i can learn?