我试图在 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/