如何将指针从main传递给函数?

时间:2014-03-05 17:52:50

标签: c++ arrays pointers pass-by-reference insertion-sort

基本上,我尝试根据输入设置数组的大小(将向用户请求numArraySize)。我在main中创建了一个数组指针,并希望将此指针传递给函数。

#include <iostream>
using namespace std;

// Algorithm for Insertion Sort
int numArraySize; 

void inputNums(int numArray[])
{
    cout << "Enter a bunch of numbers: " ;
    for(int x=0; x<numArraySize; x++)
    {
        cin >> numArray[x];
    }
}
void outputNums(int numArray[])
{
    for(int x=0; x<numArraySize; x++)
    {
        cout << numArray[x];
        if(x != numArraySize-1)
        {
            cout << " - ";
        }
    }
}
void insertionSort(int numArray[])
{
    int num;
    int i;
    for(int j=1; j<numArraySize; j++)
    {
        num = numArray[j];
        i = j-1;
        while(i>=0 && numArray[i] > num)
        {
            numArray[i+1] = numArray[i];
            i--;
        }
        numArray[i+1] = num;
    }
}

int main()
{
    int *numbers = new int[numArraySize];
    int choice;

    cout << "1) Insertion Sort" << endl;
    cout << "Enter your choice: " << endl;

    cin >> choice;              // Input choice for which algorithm to use

    if(choice == 1)
    {
        cout << "Enter size of the array: ";
        cin >> numArraySize;
        inputNums(numbers);         // Insert numbers
        insertionSort(numbers);     // Use algorithm to sort then output
        outputNums(numbers);
    }

    cout << endl;
    return 0;
}

编辑:

好的,我修复了错误。我变了: &#34; int numArraySize = 1;&#34; 和...的位置 &#34; int * numbers = new int [numArraySize];&#34;

问题是我不得不初始化numArraySize。这显然会导致错误。第二个问题是我必须在初始化之前输入numArraySize &#34; int *数字&#34;在主要功能。

    #include <iostream>
    using namespace std;

    // Algorithm for Insertion Sort
    int numArraySize=1; 

    void inputNums(int numArray[])
    {
        cout << "Enter a bunch of numbers: " ;
        for(int x=0; x<numArraySize; x++)
        {
            cin >> numArray[x];
        }
    }
    void outputNums(int numArray[])
    {
        for(int x=0; x<numArraySize; x++)
        {
            cout << numArray[x];
            if(x != numArraySize-1)
            {
                cout << " - ";
            }
        }
    }
    void insertionSort(int numArray[])
    {
        int num;
        int i;
        for(int j=1; j<numArraySize; j++)
        {
            num = numArray[j];
            i = j-1;
            while(i>=0 && numArray[i] > num)
            {
                numArray[i+1] = numArray[i];
                i--;
            }
            numArray[i+1] = num;
        }
    }

    int main()
    {
        int choice;

        cout << "1) Insertion Sort" << endl;
        cout << "Enter your choice: " << endl;

        cin >> choice;              // Input choice for which algorithm to use

        if(choice == 1)
        {
            cout << "Enter size of the array: ";
            cin >> numArraySize;
            int *numbers = new int[numArraySize];
            inputNums(numbers);         // Insert numbers
            insertionSort(numbers);     // Use algorithm to sort then output
            outputNums(numbers);
        }

    cout << endl;
    system("pause");
    return 0;
}

感谢您的帮助:)

2 个答案:

答案 0 :(得分:4)

要将数组传递给函数,只需使用int *array

即可
void outputNums(int *numArray)
{
    for(int x=0; x<numArraySize; x++)
    {
        cout << numArray[x];
        if(x != numArraySize-1)
        {
            cout << " - ";
        }
    }
}

请注意,最好使用像a vector这样的标准容器。

答案 1 :(得分:0)

在行中为数组分配内存时

int *numbers = new int[numArraySize]

,您仍然没有在变量numArraySize中获得所需的数组大小。