Passing Arrays to Functions

Hey everyone,
I just started learning C++ this semester and cplusplus.com (specifically the forum) has been an invaluable resource for me. I have a quick question I was hoping you guys could help me with. We just started learning arrays and our assignment requires us to create a program that asks the users for 5 integers to input into an array and order them in ascending and descending order (I'm putting the ordering aside at the moment).

My issue is that I can't figure out how to properly pass an array to a function. I have int main(), void InputArray (which accepts the user input), and void OutputArray (which outputs the contents of the array. The next step would be ordering, but I can't seem to pass the input function to the output function, then both to int main ().

I would greatly appreciate any help you guys could provide. Thank you very much.
Here is a piece of my code. I left the irrelevant functions out. I just want to be able to pass the input to the output and both to int main().


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void InputArray ();
void OutputArray ();
void Swap (int, int);
void SortIntegers(int, int, int);

const int ARRAY_MAX = 6; // Constant is 6 because we are accepting exactly 5 integers (0,1,2,3,4,5)

int main ()
{
    
    InputArray(intNumbersArray[intCounter]);
    OutputArray(intNumbersArray[intCounter]);

    system("pause");
    return 0;  
}
  
void InputArray (intNumbersArray[])
{

    int intNumbersArray [ARRAY_MAX] = {0}; 
    int intCounter = 0;
   
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
         cout << "Enter an integer: " << endl;
         cin >> intNumbersArray[intCounter]; 
    }
  
}

void OutputArray(int intNumbersArray[intCounter])
{
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
    cout << outputArray[intCounter] << endl;
    }   
       
}
Last edited on
What exactly have you tried? Could you provide a code example of your attempt?

EDIT: Ninja'd...

Ok, there are several issues with your code. First off, line 13 is somewhat wrong, ARRAY_MAX should be 5, not 6 (0, 1, 2, 3, 4: = 5 numbers). Next, there should be spaces between the date types and the variable names. Ex: intNumbersArray[] should be int NumbersArray[]. Also, when specifying a size for an array, you must use a constant value (or a const variable). So line 39 is wrong, just take the "intCounter" out of the brackets. Last, your calling the functions wrong. You must declare an array inside of main(), then pass it to the functions.

Ex.
1
2
3
4
5
6
7
int main()
{
    int array[ARRAY_MAX];
    InputArray(array);
    OutputArray(array);
    return 0;
}


Another small issue, in your InputArray() function, you create a variable called "intCounter", then your create another variable with the same name inside the for loop. Either take out line 29, or remove the "int" from the for loop on line 31. Its not really a major issue now, but something like that could cause major headaches later on.
Last edited on
Thank you very much for your help ModShop. My teacher insists that all variables/arrays begin with their type...so intNumbersArray is the name of the array. Other than that, I made the fixes you suggested, but I'm still unclear of how to pass the functions properly. In the code you posted:

1
2
3
4
5
6
7
int main()
{
    int array[ARRAY_MAX];
    InputArray(array);
    OutputArray(array);
    return 0;
}


Should intNumbersArray be replacing "array" ? I made other attempts/fixes, but to no avail.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void InputArray ();
void OutputArray (int[]);
void Swap (int, int);
void SortIntegers(int, int, int);

const int ARRAY_MAX = 5; // Constant is 5 because we are accepting exactly 5 integers (0,1,2,3,4)

int main ()
{
    int array[ARRAY_MAX];
    InputArray(intNumbersArray);
    OutputArray(intNumbersArray);
    
    system("pause");
    return 0;  
}
  
void InputArray ()
{

    int intNumbersArray [ARRAY_MAX] = {0}; 
       
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
        cout << "Enter an integer: " << endl;
        cin >> intNumbersArray[intCounter]; 
    }
  
}

void OutputArray(int intNumbersArray[])
{
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
        cout << intNumbersArray[intCounter] << endl;
    }   
       
}
InputArray should have int intNumbersArray[] in its parameter list (the parenthesis). When you call the functions, you pass the name of the array your created inside of main(), not the name of the argument of the function.
Now I'm getting this error on line 28.

Line 28: declaration of 'int intNumbersArray[5]' shadows a parameter
Because your re-declaring the array that was declared in the parameter list. Just remove line 28.
There must be something else inherently wrong with the way I'm passing the functions. After the first input, the program freezes and shuts down. I have attempted running it with several compilers and I get the same results.

This is the current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void InputArray (int[]);
void OutputArray (int[]);
void Swap (int, int);
void SortIntegers(int, int, int);

const int ARRAY_MAX = 5; // Constant is 5 because we are accepting exactly 5 integers (0,1,2,3,4)

int main (int intNumbersArray[])
{
    int array[ARRAY_MAX];
    InputArray(intNumbersArray);
    OutputArray(intNumbersArray);
    
    system("pause");
    return 0;  
}
  
void InputArray (int intNumbersArray[])
{

    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {
        cout << "Enter an integer: " << endl;
        cin >> intNumbersArray[intCounter]; 
    }
  
}

void OutputArray(int intNumbersArray[])
{
    for (int intCounter = 0; intCounter < ARRAY_MAX; intCounter++)
    {   
        cout << intNumbersArray[intCounter] << endl;
    }   
       
}
int main (int intNumbersArray[])

Even the craziest backwater compiler shouldn't accept that as valid.
Hanst99,
Any suggestions are appreciated. I am in the process of learning C++ and am having some fundamental issues with understanding the passing of functions... specifically with arrays.

Thank you in advance.
I figured it out. I was being an idiot. I copied a piece of code ModShop suggested without changing "array" to intNumbersArray. Once I declared it inside of main, and took out the parameters in main it worked.

Thank you hanst99 and ModShop. Your help is greatly appreciated.
closed account (S6k9GNh0)
intNumbersArray is really a bad name.

int main (int intNumbersArray[])

Even the craziest backwater compiler shouldn't accept that as valid.


Actually, this is understandable. It isn't the compiler that wants main defined, its the linker because the C/++ runtime calls for it. As long as the symbol "main" is defined, the compiler couldn't care less. Most compilers, for instance, don't care that you define main as void main() nor do they even give errors/warnings (by default). This is understandable because of the nature of how the main function is called.

However, let me make clear to whoever reads this that the runtime expects something in return (as it's said in the standard) and you should give it what it wants. To do otherwise is undefined behavior and bad practice.
Last edited on
Topic archived. No new replies allowed.