Sending an array between two functions C++

967 Views Asked by At

I am trying to send a array of 15 integers between two functions in C++. The first enables the user to enter taxi IDs and the second functions allows the user to delete taxi IDs from the array. However I am having an issue sending the array between the functions.

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

Any help is great valued. Many thanks.

4

There are 4 best solutions below

4
nachbar On BEST ANSWER

Since the array has been created on the stack, you would just need to pass the pointer to the first element, as an 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);
}

Since the array is created on the stack, it will no longer exist once the routine in which it was created has exited.

0
flavian On
void endShift (int* arr)
{
    arr[0] = 5;
}
0
pmr On

Declare the array outside of those functions and pass it to them by reference.

void startShift(int (&shifts)[15]) {
 // ...
}
void endShift(int (&shifts)[15]) {
 // ...
}

int main() {
  int array[15];
  startShift(array);
  endShift(array);
}

This isn't exactly pretty syntax or all that common. A much more likely way to write this is to pass a pointer to the array and its length.

void startShift(int* shifts, size_t len) {
  // work with the pointer
}

int main() {
  int array[15];
  startShift(array, 15);
}

Idiomatic C++ would be different altogether and use iterators to abstract away from the container, but I suppose that is out of scope here. The example anyway:

template<typename Iterator>
void startShift(Iterator begin, Iterator end) {
  // work with the iterators
}

int main() {
  int array[15];
  startShift(array, array + 15);
}

You also wouldn't use a raw array, but std::array.

0
Dietmar Kühl On

It won't work to use a local array in the startShift() function. You are best off to do one or more of the following:

  1. Use an array in the function calling startShift() and endShift() and pass the array to these functions, e.g.:

    void startShift(int* array) { ... }
    void endShift(int* array) { ... }
    int main() {
        int arrray[15];
        // ...
        startShift(array);
        // ...
        endShift(array);
        // ...
    }
    
  2. Don't use built-in arrays in the first place: use std::vector<int> instead: that class automatically maintains the current size of the array. You can also return it from a function altough you are probably still best off to pass the objects to the function.