简体   繁体   English

发送动态指针数组以使用C ++

[英]send dynamic array of pointer to function in c++

I trying to send array to function, but my program gets stuck 我试图将数组发送给函数,但是我的程序被卡住了

int main()
{
    int n, i;
    bool random;

    cout << "number of elements in array:"; cin >> n;
    cout << "use random values?"; cin >> random;

    int* arr = new int[n]; //create int array with n size
    init_array(random, n, arr); //fill array using function

    for (i = 0; i <= n; i++) //display array
        cout << " " << arr[i];

    return 0;
}

This function should fill array with random number or input from keyboard 此函数应使用随机数或键盘输入来填充数组

void init_array(bool random, int n, int* arr)
{
    int i;
    if (random)
    {
        srand(time(0)); //initialize random;
        for (i = 0; i < n; i++)
            arr[i] = rand() % 100;
    }
    else
        for (i = 0; i<n; i++)
            cout << "arr[" << i << "]: "; cin >> arr[i];
}

Is there any way send dynamic array to function? 有什么办法可以发送动态数组来起作用吗?

When you do not use brackets after your for-loop, only the first statement is used as a loop: 如果在for循环后不使用方括号,则仅将第一条语句用作循环:

else
    for (i = 0; i<n; i++)
        cout << "arr[" << i << "]: "; cin >> arr[i];

This loop will attempt to print "arr[#]" n times, and then ask for an input (which will attempt to be placed in the item 1 after the last element in your array (UB). 此循环将尝试打印n次“ arr [#]”,然后要求输入(它将尝试放置在数组(UB)中最后一个元素之后的第1项中)。

What you want is this: 您想要的是:

else
{
    for (i = 0; i<n; i++)
    {
        cout << "arr[" << i << "]: "; 
        cin >> arr[i];
    }
}

You also have a problem with your output: 您的输出也有问题:

for (i = 0; i < n; i++) // <= would attempt to print 1 more item than exists in the array

And just for completeness, most of these issues go away when you use a container that does all of this for you: 仅出于完整性考虑,当您使用为您完成所有这些工作的容器时,这些问题中的大多数都会消失:

int main()
{
    int n = 0;
    bool useRandom = false;
    std::cout << "number of elements in array:"; 
    std::cin >> n;
    std::cout << "use random values?"; 
    std::cin >> useRandom;

    std::vector<int> arr(n);
    init_array(useRandom, arr);

    std::copy(arr.begin(), arr.end(), std::ostream_iterator<int>(std::cout, " "));

    return 0;
}

void init_array(bool useRandom, std::vector<int>& vec)
{
    ::srand(time(0)); //initialize random;
    int n = 0;
    std::transform(vec.begin(), vec.end(), vec.begin(), [&](int i)
    {
        if (useRandom)
        {
            i = rand() % 100;
        }
        else
        {
            std::cout << "arr[" << n++ << "]:  ";
            std::cin >> i;
        }
        return i;
    });
}

Your code is asking for a number at the end because of last cin>>n 您的代码要求最后输入一个数字,因为最后一个cin>>n

fix else part in init_array as: init_array else部分修复为:

 else
        for (i = 0; i<n; i++)
            { //Notice braces
             cout << "arr[" << i << "]: ";
             cin >> arr[i];
            }

Also fix: 同时解决:

for (i = 0; i < n; i++) //display array from index 0 to n-1
    cout << " " << arr[i];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM