I have two structs.
typedef struct Side Side;
struct Side{
bool taken;
unsigned color;
};
typedef struct{
Side* sides;
}Cube;
I want to make an array of 100 cubes dynamically each with 3 sides - which also needs to be dynamic. What is the proper way of doing this?
void generateCube(Cube** cubes, int size, int (*calculateFunction)(int)){
*cubes = (Cube*)malloc(sizeof(Cube) * size);
Cube* cubeIterator = *cubes;
Cube* endCube = *cubes + sizeof(Cube) * size;
unsigned sideIndex = 1;
for(endCube; cubeIterator != endCube; cubeIterator += sizeof(Cube)){
cubeIterator->sides = (Side*)malloc(sizeof(Side) * 3);
cubeIterator->sides[0].color = (*calculateFunction)(sideIndex++);
cubeIterator->sides[1].color = (*calculateFunction)(sideIndex++);
cubeIterator->sides[2].color = (*calculateFunction)(sideIndex++);
}
}
This is what I came up with but the values being assigned to the color are not correct. I am new to C please go easy on me :)