1

I'm trying to pass an array as a parameter using this function. In the function, I let the user enter how big the array is, as well as the values in said array. What I can't figure out is how to declare the variables in main that will allow me to use the function in main, and more specifically, how do I declare the array variable in main without knowing the size beforehand (user enters size in function).

void arrayFunction(int array1[], int arraySize);
    
int main() {
    
    int arrayLength;
    int arrayMain[];
    cout << "Enter length of array: " << endl;
    cin >> arrayLength;
        
    arrayFunction(arrayMain, arrayLength);
    
    return 0;
}

void arrayFunction(int array1[], int arraySize)
{
    cout << "Enter length of array: " << endl;
    cin >> arraySize;
    
    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << i + 1 << endl;
        cin >> array1[i];
    }
}
Quxflux
  • 3,133
  • 2
  • 26
  • 46
asarabs
  • 11
  • 1
  • 6
    You can't. Use `std::vector`, if you don't know the size at compile time. See e.g. https://stackoverflow.com/questions/10865861/when-to-use-vectors-and-when-to-use-arrays-in-c – Bob__ Jun 30 '21 at 15:27
  • 1
    C++ requires the size of an array to be a compile time constant expression. The rationale is that *normal* progammers (meaning when not writing the standard library modules) should use `std::vector` instead. – Serge Ballesta Jun 30 '21 at 15:33
  • The easy way is to use `std::vector`. The hard way is to write a program that when the size becomes known it will output source code to a compiler and then run that resulting executable. (The hard way is similar to how Oracle generates code for certain queries to make them run really fast.) – Eljay Jun 30 '21 at 16:21

2 Answers2

0

As the comments say, your best bet is to use a std::vector<int> instead:

#include <vector>

std::vector<int> arrayFunction(){
    int arraySize;
    cout << "Enter length of array: " << endl;
    cin >> arraySize;

    std::vector<int> result(arraySize);

    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << (i + 1) << endl;
        cin >> result[i];
    }
    return result;
}

If you don't have access to std::vector, you'll need to build something similar use dynamic allocated arrays:

int* arrayFunction() {
    ...
    // instead of the std::vector<int> line, you can just do this:
    int* result = new int[arraySize];
    ...
}

but you'll need to make sure to call delete [] array; when you're done with the memory. std::vector will do this automatically so it's generally a safer approach to dynamic arrays.

mattlangford
  • 1,260
  • 1
  • 8
  • 12
0

It would be great to use vector. But you can also use a traditional pointer to achieve this so-called dynamic array. This array size can be then specified during runtime instead of compile time.

Just modified a little from your code:

#include<iostream>
using namespace std;

void arrayGenerator(int* , int );
void arrayPrinter(const int* pArray, int arraySize);

int main() {
    int arrayLength;
    cout << "Enter length of array: " << endl;
    cin >> arrayLength;
    int* pArray = new int[arrayLength];
    arrayGenerator(pArray, arrayLength);
    arrayPrinter(pArray, arrayLength);
    delete [] pArray;
    return 0;
}
void arrayGenerator(int* pArray, int arraySize) {
    for(int i = 0; i < arraySize; i++)
    {
        cout << "Enter value #" << i + 1 << endl;
        cin >> pArray[i];
    }
}

void arrayPrinter(int* pArray, int arraySize) {
    cout << "This is the given array: \n";
    for (int i = 0; i < arraySize; i++)
        cout << i << " ";
    cout << endl;
}
ripfreeworld
  • 143
  • 1
  • 13
  • 1
    Tradition is not a good trait in programing. In fact explicit use of operator `new` and `delete` is discourage since C++11/C++14 (make_unique) and RAII pattern is recommended. – Marek R Jun 30 '21 at 16:10