Question

I have a function that expects a pointer array as such:

void SortResistance(MyClass ** pointerArray, int arraySize);

and does a bubble sort. Inside that function I can access any two array elements using:

MyClass * item1 = *pointerArray + i;  
MyClass * item2 = *pointerArray + i + 1;

Where I'm iterating over the array. So far so good. When I swap the values using a very simple swap function such as:

void Swap(MyClass ** item1, MyClass ** item2);

The values on the pointers swap as expected.

My problem is that I don't know how to reassign them back to pointerArray.

This doesn't work:

*(pointerArray + i) = item1;
*(pointerArray + i + 1) = item2;

(Well, it works when i equals 0 but otherwise it just shifts the pointee, not its value.)

I'm wondering if anyone would please help me with this problem. The function prototypes cannot be changed so I'm assuming they are correct.

Here is the gory detail in the simplest possible implementation. Thank you very much for your responses.

I was asked to look at something similar to the following declarations and to implement:

#include <string>
using namespace std;

void main()
{
    //TODO: instantiate an array of pointers and sort them using the below
};

class MyClass
{
public:
    double value;
    string name;
}

void Sort(MyClass ** myArray, int arraySize)
{
    //TODO: implement bubble sort
}

void Swap(MyClass ** pointer1, MyClass ** pointer2)
{
    MyClass *temp = *pointer1;
    *pointer1 = *pointer2;
    *pointer2 = temp;
}

My solution, based on something like the following http://www.cplusplus.com/reference/algorithm/sort/ would be something like this:

#include <string>
using namespace std;

class MyClass
{
public:
    double value;
    string name;
};

void Swap(MyClass ** pointer1, MyClass ** pointer2)
{
    MyClass *temp = *pointer1;
    *pointer1 = *pointer2;
    *pointer2 = temp;

}

void Sort(MyClass ** myArray, int arraySize)
{
    bool done = false; // this flag will be used to check whether we have to continue the algorithm

    while (!done)
    {
        done = true; // assume that the array is currently sorted
        for (int i = 0; i < arraySize - 1; i++) // for every element in the array  
        {
            MyClass * p1 = *myArray + i;
            MyClass * p2 = *myArray + i + 1;

            //MyClass * p1 = *(myArray + i);
            //MyClass * p2 = *(myArray + i + 1);
            //MyClass * p1 = myArray[i];
            //MyClass * p2 = myArray[i + 1];

            if ( p1->value > p2->value ) // compare the current element with the following one
            {
                // They are in the wrong order, swap them
            Swap(&p1, &p2);
                //Swap(*(&myArray + i), *(&myArray + i + 1));
            //Swap(myArray + i, myArray + i + 1);

            *(myArray + i) = p1;
            *(myArray + i + 1) = p2;

                done = false; // since we performed a swap, the array needs to be checked to see if it is sorted
                               // this is done in the next iteration of the while
            }
        }
    }
}

void main()
{
    MyClass item1; item1.name = "item1"; item1.value = 25.5;
    MyClass item2; item2.name = "item2"; item2.value = 15.5;
    MyClass myItems[2]; myItems[0] = item1; myItems[1] = item2;

    MyClass * myPointerToItemArray = myItems;
    Sort(&myPointerToItemArray, 2);
}

The code compiles fine under VS 2010. As you can see, everything goes well until I have to reassign the new set of pointers to the array. Any advice would be greatly appreciated. I'm starting to think the declarations have to be modified to make this work.

Was it helpful?

Solution 4

After quite a while I was able to make it work. The code for the example problem above is given here. Thank you all for your suggestions.

#include <string>
using namespace std;

class MyClass
{
public:
    double value;
    string name;
};

void Swap(MyClass ** pointer1, MyClass ** pointer2)
{
    MyClass *temp = *pointer1;
    *pointer1 = *pointer2;
    *pointer2 = temp;

}

void Sort(MyClass ** myArray, int arraySize)
{
    //sorting
    bool done = false;

    while (!done)
    {
        done = true; // assume that the array is currently sorted
        for (int i = 0; i < arraySize - 1; i++) // for every element in the array  
        {
            if (myArray[i]->value > myArray[i + 1]->value) 
            {
                Swap(&myArray[i], &myArray[i + 1]);
                //MyClass temp = *myArray[i];
                //*myArray[i] = *myArray[i + 1];
                //*myArray[i + 1] = temp;
                done = false;
            }

        }

    }

};

void main()
{
    MyClass item1; item1.name = "item1"; item1.value = 25.5;
    MyClass item2; item2.name = "item2"; item2.value = 15.5;
    MyClass item3; item3.name = "item3"; item3.value = 10.5;
    MyClass * myItems[3]; myItems[0] = &item1; myItems[1] = &item2; myItems[2] = &item3;

    MyClass ** dp = myItems;

    Sort(dp, 3); 

}

OTHER TIPS

It's a lot easier if you treat it as an actual array rather than a pointer, e.g.

MyClass* item1 = pointerArray[i];
MyClass* item2 = pointerArray[i + 1];

and:

pointerArray[i] = item1;
pointerArray[i + 1] = item2;

You don't need to modify the function prototypes to do this.

Alright, I've done a little debugging in my own compiler... try

MyClass * item1 = *(pointerArray + i);
MyClass * item2 = *(pointerArray + i + 1);

And leave the rest of the code the same.

try catching the array values in a reference:

MyClass &* item1 = *pointerArray + i;
MyClass &* item2 = *pointerArray + i + 1;

then you shouldn't need to re-assign the values after the fact.

EDIT: What you were doing is essentially making a copy to the pointers in item1, item2. Swapping them swapped the copies, but not the original. You could try omitting the copies and just doing something like this:

Swap(&(*pointerArray + i), &(*pointerArray + i + 1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top