In the first stage, i've created an object Planet with some attributes, like name
, type
and distanceToEarth
. I've created a Repository then, basically a structure consisting of a dynamic array elems and its length and maximum capacity.
typedef enum {
NEPTUNE_LIKE,
GAS_GIANT,
TERRESTRIAL,
SUPER_EARTH,
UNKNOWN
}PlanetType;
typedef struct {
char name[30];
PlanetType type;
float distanceToEarth;
}Planet;
Planet createPlanet(char name[], PlanetType type, double distance) {
Planet pl;
strcpy(pl.name, name);
pl.distanceToEarth = distance;
pl.type = type;
return pl;
}
typedef struct
{
Planet* elems; /** dynamic array containing the planets */
int length; /** actual length of the array */
int capacity; /** maximum capacity of the array */
} PlanetRepo;
PlanetRepo createPlanetRepo(int capacity) {
/// create a new planet repo; the elems field must be dynamically allocated (malloc)
PlanetRepo r;
r.capacity = capacity;
r.length = 0;
r.elems = (Planet*) malloc(sizeof(Planet)*capacity);
return r;
}
bool remove(PlanetRepo* repo, Planet pl) {
/// @todo remove planet pl from the repository
/// return false if the planet does not exist in the repository
return false;
}
My problem is related to the function remove(). I can't figure out how I am supposed to remove that object from a dynamically allocated array.
Of course, this is not the entire code, but I've selected only the relevant parts. If I forgot to include something, let me know.