0

I see a couple similar questions but they don't have answers for my specific case. I simply need to swap the location of two elements in an array of structs, but everything I've tried so far will only copy one or the other. I believe the issue lies in the swap function.

I need do do this without the use of standard library functions. Here's what I have:

struct RentalCar {
    int year;
    char make[STRLEN];
    char model[STRLEN];
    float price;
    bool available;
};

void carCopy(RentalCar *dest, RentalCar *source) { // dest = source
    dest->year = source->year;
    myStrCpy(dest->make, source->make);            // copy c-strings
    myStrCpy(dest->model, source->model);
    dest->price = source->price;
    dest->available = source->available;
}

void carSwap(RentalCar *car1, RentalCar *car2) {
    RentalCar *carTemp = car1;
    carCopy(carTemp, car2);   // tried different orders, only copy no swap
    carCopy(car2, car1);
}

char *myStrCpy(char *dest, const char *source) {    // copy cstring
    char *origin = dest;                            // non iterated address
    while (*source || *dest) {
        *dest = *source;
        dest++;
        source++;
    }
    return origin;  // could return void instead
}
1
  • carSwap() needs to create an actual object, not just a pointer. Since carTemp points at car1, the function is equivalent to carCopy(car1, car2); carCopy(car2, car1); whih has the net effect of setting the contents of car1 and car2 to the original values of car2. To fix, create an object not a pointer, then do three copies (e.g. car1 to the carTemp, car2 to car1, then carTemp to car2). Commented Jun 9, 2018 at 0:46

2 Answers 2

1

You're mixing shallow copies with deep copies:

void carSwap(RentalCar *car1, RentalCar *car2) {
    RentalCar *carTemp = car1; // shallow copy
    carCopy(carTemp, car2);   // deep copy: overrides carTemp AND car1
    carCopy(car2, car1); // car1 and car2 already have the same values
    // original car1 data is lost
}

Because carSwap is given RentalCar* (and not RentalCar**) we can't change the user's pointer, so we must stick to deep copies.

We could do this (and still use the other functions):

void carSwap(RentalCar *car1, RentalCar *car2) {
    RentalCar carTemp = *car1;
    carCopy(car1, car2);
    carCopy(car2, &carTemp);
}

But we don't need the other functions. We can just do this:

void carSwap(RentalCar *car1, RentalCar *car2) {
    RentalCar carTemp = *car1;
    *car1 = *car2;
    *car2 = carTemp;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That actually does work, I must've been doing something out of order the first time I tried to shallow copy it
The array only has 5 elements and its for a school assignment, I'm not worried about efficient use of memory here. But out of curiosity, how would I do this without creating a new temporary object?
Here pointers are useful as they're just an address, so the making/copying of pointers requires no construction: RentalCar * array[5]; // an array of pointers to structures // ... // add objects to your array //... // The swapping will be similar to before: RentalCar * tmp = array[i]; array[i] = array[j]; array[j] = tmp;
1

In carSwap, carTemp is a pointer pointing to the same memory location as car1. The line

carCopy(carTemp, car2);   // tried different orders, only copy no swap

overwrites car1 with car2.

Change carSwap to this:

void carSwap(RentalCar *car1, RentalCar *car2) {
    RentalCar carTemp;
    carCopy(&carTemp, car2);
    carCopy(car2, car1);
    carCopy(car1, &carTemp);
}

1 Comment

I see the problem, what's the solution? I tried creating a new carTemp object but it just crashes on the function call.

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.