C++ 在函数参数中使用 ** 指针不起作用

标签 c++ visual-c++-2010

我有一个需要指针数组的函数:

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

并进行冒泡排序。在该函数中,我可以使用以下方法访问任意两个数组元素:

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

我在哪里遍历数组。到目前为止,一切都很好。当我使用非常简单的交换函数交换值时,例如:

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

指针上的值按预期交换。

我的问题是我不知道如何将它们重新分配回 pointerArray。

这行不通:

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

(好吧,它在 i 等于 0 时起作用,否则它只是移动指针对象,而不是它的值。)

我想知道是否有人可以帮助我解决这个问题。函数原型(prototype)无法更改,因此我假设它们是正确的。

这是最简单的实现中的血淋淋的细节。非常感谢您的回复。

我被要求查看类似于以下声明的内容并实现:

#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;
}

我的解决方案,基于类似以下的内容 http://www.cplusplus.com/reference/algorithm/sort/会是这样的:

#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);
}

代码在 VS 2010 下编译良好。如您所见,一切顺利,直到我必须将新的指针集重新分配给数组。任何建议将不胜感激。我开始认为必须修改声明才能使其正常工作。

最佳答案

如果你把它当作一个实际的数组而不是一个指针,那就容易多了,例如

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

和:

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

您无需修改​​函数原型(prototype)即可执行此操作。

关于C++ 在函数参数中使用 ** 指针不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994849/

相关文章:

java - Android Studio 中的断点 C++ 文件

c++ - 如何获得时区

c++ - 如何在 ArangoDb AQL 查询中指定数据库?

c++ - 我可以从另一个函数中初始化静态成员吗​​?

c++ - 如何在C++中对数学运算使用引用/指针?

c++ - Visual C++ 2010 fatal error C1083;没有权限

c++ - 用 C++ 建模服装

visual-c++-2010 - 无法卸载VC++2010 : Error: A newer version of Microsoft Visual C++ 2010 Redistributable has been detected on the machine

c - 调试 Visual Studio 2010 DLL 项目

visual-c++-2010 - 如何在 Visual C++ 2010 中禁用代码折叠