2

I have this assignment for my Algorithms class to write a system for a car dealership that allows them to search through their inventory so that the complexity of the search is not O(n). To solve this I chose to use a binary search tree that points to the different models of cars and then using three different BSTs to allow for searches of each model for price, mileage and year.

The BSTs are built using an array implementation, where the left child of a parent is 2* the parent index + 1, and the right child is 2* the parent index + 2.

the three structures I am using are defined as such:

typedef struct car {

    int inventoryID;
    char* make;
    char* model;
    int year;
    int mileage;
    int price;

} car;

typedef struct car_BST {

    car (**cars)[3];
    int size;

} car_BST;

typedef struct BST {

    car_BST** carModel;
    int size;

} BST;

So when I try to add a car to each tree i follow this general step by step process:

while(1){
    if (tmp->cars[val][i[val]] == NULL){
        tmp->cars[val][i[val]] = newCar;
        break;
    }

    if (i[val] > tmp->size)
        incrCar_BSTSize(tmp, i[val]);

    if (newCar->price >= tmp->cars[val][i[val]]->price)
        i[val] = 2 * i[val] + 2;
    else
        i[val] = 2 * i[val] + 1;
}

Yet the compiler reports an error that says "assignment to expression with array type"

Is there something obvious that I have missed or am I just doing something fundamentally wrong?

2 Answers 2

2

You're trying to dereference tmp->cars[val][i[val]], which essentially means *(*(tmp->cars + val) + i[val]), or similar to just **(tmp->cars) when the indices are all zeros.

car_BST::cars is defined as a pointer to pointer to array of 3 cars.

So **(tmp->cars) is a car array, hence you cannot assign a value to it.

Also it is a car array, not car* array. It means you can never assign newCar (which I assume has a type of car*) into **(tmp->cars)[0] either.

I'm not quite sure what you are trying to achieve here. Maybe you want car_BST::cars to be car* cars[3] instead? Or car* (**cars)[3].

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

2 Comments

What I want to achieve is to make 3 arrays of car** which is why the car_BST consists of 3 arrays of car** which would be 3 arrays of arrays of type car*. From my understanding, that the right implementation, but I have clearly been wrong before
Is it "3 arrays of car**s" or "an array of 3 car**s"? For the latter it should be car** cars[3].
1

You can use typedefs to simplify your task.

A single array-based tree that stores pointers would look like this:

typedef car** car_bst;

It's just an array of pointers.

An array of three such things would look like this:

car_bst cars[3];

Now if you think about it a bit, this is equivalent to

car **cars[3];

and not to what you wrote.

Please note that you can often hear on the internets that you should never hide pointers behind typedefs. Take this advice with caution. It is only good most of the time, not all of the time.

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.