I have a simple struct:
typedef struct {
void *things;
int sizeOfThings;
} Demo;
things is intended to contain an array of individual "thing", like maybe strings or ints. I create a pointer to it:
Demo * Create(int value) {
Demo *d = malloc(sizeof(Demo));
if (d != NULL) {
d->sizeOfThings = value;
d->things = malloc(20 * value); // We'll have a max of 20 things
}
}
value
is sizeof(int) for an array of ints, for example.
If in another function I want to insert something into d->things (assuming at least for not that I'm just adding it to the first slot, position management done elsewhere):
char * thing = "Me!";
strncpy(d->things[0], &thing, d->sizeOfThings);
I get around the strncpy area
test.c:10: warning: pointer of type ‘void *’ used in arithmetic
test.c:10: warning: dereferencing ‘void *’ pointer
test.c:10: error: invalid use of void expression
I'm just trying to understand the use of void* as a way to generalize my functions. I suspect there's something wrong with d->things[0]
.