1

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.

5
  • 9
    Are you sure you are not writing a C program instead of a C++ program? Usage of malloc, typedef struct, etc. are indications that you are writing C, not C++. Commented Mar 28, 2020 at 20:32
  • @PaulMcKenzie You're right. I've simultaneously studied both C and C++ and now I tend to mix them up Commented Mar 28, 2020 at 20:35
  • 6
    Well, with C++, all you need is std::vector<Planet> and be done. Commented Mar 28, 2020 at 20:36
  • 4
    @SeverienneBianca That is a really bad idea. Proper style and idioms in both languages are completely different. The code you are showing would be a badly written C++ program and would technically even have undefined behavior in C++ prior to C++20. You should focus on one of the two languages and what you are showing right now is how one writes C, not C++. Commented Mar 28, 2020 at 20:36
  • 6
    This is not C++. Nor is this a mix of C and C++. This is C. Commented Mar 28, 2020 at 20:36

3 Answers 3

4

Since you insisted on tagging C++, rather than C:

In C++ you wouldn't define the PlanetRepo and the associated functions at all. Instead you would simply declare a variable of type

std::vector<Planet>

or maybe depending on the use case (but less likely)

std::list<Planet>

Both of these already have member functions .erase that remove elements from them.


In C++ you also wouldn't write

typedef struct {
    char name[30];
    PlanetType type;
    float distanceToEarth;
}Planet;

but instead

struct Planet {
    char name[30];
    PlanetType type;
    float distanceToEarth;
};

and you would most likely use std::string instead of char[30] as type for name.

Instead of a function Planet createPlanet(char name[], PlanetType type, double distance) you would define a constructor for Planet:

struct Planet {
    std::string name;
    PlanetType type;
    float distanceToEarth;
    Planet(std::string name, PlanetType type, double distance)
      : name(name), type(type), distance(distance)
    {}
};

and probably make the members private.

You also wouldn't define an unscoped enum, but a scoped one instead (since C++11), see Why is enum class preferred over plain enum?.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! I've discovered that I actually have huge gaps in my C++ knowledge, so I will just stick to C instead.
@SeverienneBianca Notice that C++ isn't just "C with classes", it is very different, and if you want to use it, you should invest more time in learning just C++. It is very confusing at the start, so stick to one of these until you feel comfortable to learn a new language. Good luck!
0

Since this is rather a C than a C++ program, you could use a linked list which makes it possible for you to delete elements in a dynamically allocated "array".

This might be of interest.

Comments

0

Like mentioned before C++ has implemented data structures so you can easily store your planets. But you can do something like:

bool remove(PlanetRepo* repo, Planet pl) {
    PlanetRepo* temp = (PlanetRepo*) malloc(sizeof(Planet)*repo->capacity);
    if(!temp){
        return false;
    }
    temp->capacity = repo->capacity;
    temp->size = repo->size-1;
    for(int i = 0; i < repo->size; ++i){
        if(repo[i] != pl){ //This won't work. How to compare Planets is left as an exercise to the reader
            temp[i] = repo[i];
        }
    }
    free(repo);
    repo = temp;
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.