Error during implementation of quickSort algorithm in c++:- "error: cannot convert 'int*' to 'int**'....."

Error during implementation of quickSort algorithm in c++:- "error: cannot convert 'int*' to 'int**'....."

完整错误如下:- "|error: 无法将参数 '1' 的 'int*' 转换为 'int**' 到 'void quickSort(int**, int, int)'|"

我的全部代码如下:

#include <iostream>

using namespace std;

int Partition (int *A[], int p, int r) {
    int x = *A[r];
    int i = p-1;
    for (int j=0; j<=r; j++){
        if(*A[j]<=x){
            i++;
            int save=*A[j];
            *A[j] = *A[i];
            *A[i] = save;
        }
    }
    int save2=*A[i+1];
    *A[i+1]=*A[r];
    *A[r]=save2;
    return (i+1);
}

void quickSort(int *A[], int p, int r) {
    if (p<r){
    int q = Partition(A, p, r);
    quickSort(A, p, (q-1));
    quickSort(A, (q+1), r);
    }
}



int main() {
    int RR[] = {2,8,7,1,3,5,6,4};
    int y=sizeof(RR)/sizeof(int)-1;
    cout << y << endl;
    int *QQ = RR;
    cout << *QQ << endl;
    quickSort(QQ, 0, y);

    return 0;
}

这是我自己从伪代码中尝试的实现。我是编程新手,所以如果您能稍微说明一下为什么会发生此错误,那将会很有帮助。

提前致谢

关于代码,我首先注意到的是一大堆不必要的指针取消引用。 A 的内容将在不需要额外指针的情况下更改,因为数组会衰减为指针 (What is array decaying?),因此 A 被视为指向第一个数组元素的指针,并且您正在有效地传递已经通过引用数组。

更糟糕的是,int * A[] 不是指向 int 数组的指针,它是指向 int 的指针数组。一个非常不同的事情。 *A[0] 不 return 2,它尝试使用 2 作为地址,并且 return 内存中地址 2 处的任何内容。这几乎肯定不是你想要的任何东西,或者是允许,看到这样的程序会做一些不幸的事情。运气好就崩溃

相反,尝试

int Partition (int A[], int p, int r) {
    int x = A[r];
    int i = p-1;
    for (int j=0; j<=r; j++){
        if(A[j]<=x){
            i++;
            int save=A[j];
            A[j] = A[i];
            A[i] = save;
        }
    }
    int save2=A[i+1];
    A[i+1]=A[r];
    A[r]=save2;
    return (i+1);
}

void quickSort(int A[], int p, int r) {
    cout << p << ',' << r << endl; // Bonus: This will make the next bug really easy to see
    if (p<r){
    int q = Partition(A, p, r);
    quickSort(A, p, (q-1));
    quickSort(A, (q+1), r);
    }
}

注意 quickSort 顶部的额外 cout 语句,这将帮助您查看 Partition 中的逻辑错误。该程序将由于...而崩溃 等待它!堆栈溢出,但 cout 会告诉你原因。