c++ - 在两个函数 C++ 之间发送一个数组

标签 c++ arrays function

我试图在 C++ 中的两个函数之间发送一个包含 15 个整数的数组。第一个功能允许用户输入出租车 ID,第二个功能允许用户从数组中删除出租车 ID。但是我在函数之间发送数组时遇到问题。

void startShift ()
{
    int array [15]; //array of 15 declared 

    for (int i = 0; i < 15; i++)
    {
        cout << "Please enter the taxis ID: ";
        cin >> array[i]; //user enters taxi IDs

        if (array[i] == 0)
            break;
    }

    cout << "Enter 0 to return to main menu: ";
    cin >> goBack;
    cout << "\n";
    if (goBack == 0)
        update();
}

void endShift ()
{
    //need the array to be sent to here

    cout << "Enter 0 to return to main menu: ";
    cin >> goBack;
    cout << "\n";
    if (goBack == 0)
        update();
}

任何帮助都是非常有值(value)的。非常感谢。

最佳答案

由于数组是在栈上创建的,你只需要将指针作为一个 int* 传递给第一个元素

void endshift(int* arr)
{
int val = arr[1];
printf("val is %d", val);
}

int main(void)
{
int array[15];
array[1] = 5;
endshift(array);
}

由于数组是在堆栈上创建的,一旦创建它的例程退出,它就不再存在。

关于c++ - 在两个函数 C++ 之间发送一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927933/

相关文章:

C++ 将指针传递给模板类函数

c++ - 在 C++ 中什么时候使用 cerr 什么时候使用 cout?

c++ - Clang 模糊调用(int、float、double)

用于函数调用的 Java 正则表达式

PHP:如何计算人的月龄

javascript - 使用 javascript 显示列表项

c++ - 先进先出实现

javascript - array.push 在 forEach 循环内不起作用

python - 如何将字节数组的内容复制到列表 (Python)?

C语言 : I want to see if a value of a[] is less than all the values of b[]