为什么void sorting(int * [],int)导致“无法读取内存”?

时间:2019-02-23 04:26:32

标签: c++ sorting pointers

我正在研究使用动态分配内存的C ++程序。函数int *getNumbers(int)工作正常。

我现在想做的是获取这些信息,然后对其进行排序。当我将其发送到void sorting(int *[], int)时,未收到任何错误消息,但显示“无法读取内存”。这发生在:

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

我并不是真正的排序专家,但是与Pointers合作。为什么不能正常运行?

#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 numbers\t";
    cin >> num;

    while (num < 5 || num > 21) {
        cout << "\nPlease try again - between 5 and 20\n";
        cout << "How mamy num\t";
        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 again\n";
    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 << "\n\n";
    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 << " number\t";
        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;
    }
}

1 个答案:

答案 0 :(得分:2)

您的问题是,您要将一个指针传递给数组(指向int的指针)到sorting,在这里您只需传递数组本身。例如。删除在'&'中调用sorting的{​​{1}}并将main()函数更改为:

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