为什么 void 排序(int *[], int) 会导致"Unable to read memory"?

Why does void sorting(int *[], int) result in "Unable to read memory"?

本文关键字:int Unable to read memory 排序 void 为什么      更新时间:2023-10-16

我正在使用动态分配内存的C 程序中的程序。功能int *getNumbers(int)正常工作。

我现在要做的就是获取这些信息,然后对其进行排序。当我将其发送到void sorting(int *[], int)时,我没有收到任何错误消息,而是"无法读取内存"。这发生在:

if (*(sort[index]) < *minElem)   // Around line 122

我并不是对分类的新手,而是使用指针。为什么不正确运行?

#include <iostream>             //preprocessor directive Pg. 28 and Ch. 1
#include <string>               // Same
#include <cmath>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int *getNumbers (int);
void sorting (int *[], int);    //function that I am looking into
                                //does not work
int main ()
{
    int *numbers_2 = nullptr;
    int *numbers = nullptr;     //pointer that I use
    int num;
    cout << "How mamy numberst";
    cin >> num;
    while (num < 5 || num > 21) {
        cout << "nPlease try again - between 5 and 20n";
        cout << "How mamy numt";
        cin >> num;
    }
    numbers = getNumbers (num);
    cout << "nThe numbers are:n";
    for (int index = 0; index < num; index++) {
        cout << numbers[index] << " ";
    }
    sorting (&numbers, num);    //sorting function does not work
    cout << "nLet's try this againn";
    cout << "nThe numbers are:n";
    for (int index = 0; index < num; index++) {
        cout << numbers[index] << " ";
    }
    delete[] numbers;
    delete[] numbers_2;
    numbers = nullptr;
    numbers_2 = nullptr;
    cout << "nFinally Done!!!";
    cout << "nn";
    system ("PAUSE");
    return 0;
}
int *getNumbers (int num)
{
    int *array_Num = nullptr;
    array_Num = new int[num];
    for (int index = 0; index < num; index++) {
        cout << "Please enter " << index + 1 << " numbert";
        cin >> array_Num[index];
    }
    return array_Num;
}
void sorting (int *sort[], int size)
{
    int startScan, minIndex;
    int *minElem;
    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = sort[startScan];
        for (int index = (startScan + 1); index < size; index++) {
            if (*(sort[index]) < *minElem)  //part that I had problems
            {
                minElem = sort[index];
                minIndex = index;
            }
        }
        sort[minIndex] = sort[startScan];
        sort[startScan] = minElem;
    }
}

您的问题是,您将指针传递给数组(指向INT指针指针)到sorting,您应该简单地将数组本身传递。例如。删除您在main()中调用sorting中的'&',然后将sorting功能更改为:

void sorting (int *sort, int size)
{
    int startScan, minIndex;
    int minElem;
    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minElem = sort[startScan];
        for (int index = (startScan + 1); index < size; index++) {
            if (sort[index] < minElem)  //part that I had problems
            {
                minElem = sort[index];
                minIndex = index;
            }
        }
        sort[minIndex] = sort[startScan];
        sort[startScan] = minElem;
    }
}

示例使用/输出

$ ./bin/array_sorting
How mamy numbers        4
Please try again - between 5 and 20
How mamy num    6
Please enter 1 number   9
Please enter 2 number   2
Please enter 3 number   12
Please enter 4 number   5
Please enter 5 number   1
Please enter 6 number   3
The numbers are:
9 2 12 5 1 3
Let's try this again
The numbers are:
1 2 3 5 9 12
Finally Done!!!